-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathhaskell.code-snippets
More file actions
172 lines (172 loc) · 5.03 KB
/
Copy pathhaskell.code-snippets
File metadata and controls
172 lines (172 loc) · 5.03 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
{
"BSInteractWithSolve": {
"scope": "haskell",
"description": "(BS Version) Interact with solve function template.",
"prefix": "BS.InteractWithSolve",
"body": [
"{-# OPTIONS_GHC -Wunused-imports #-}",
"{-# LANGUAGE BangPatterns #-}",
"{-# LANGUAGE CPP #-}",
"{-# LANGUAGE OverloadedStrings #-}",
"",
"-- {-# OPTIONS_GHC -DATCODER #-}",
"import Data.ByteString.Char8 qualified as BS",
"import Debug.Trace (traceShowId)",
"",
"#ifdef ATCODER",
"debug :: Bool ; debug = False",
"#else",
"debug :: Bool ; debug = True",
"#endif",
"",
"dbgId :: (Show a) => a -> a",
"dbgId x",
" | debug = traceShowId x",
" | otherwise = x",
"",
"readInt :: BS.ByteString -> Int",
"readInt bs =",
" case BS.readInt bs of",
" Just (x, _) -> x",
" Nothing -> error \"input is not integer\"",
"",
"solve :: [Int] -> [Int]",
"solve xs = dbgId $ map (subtract 0) xs",
"",
"main :: IO ()",
"main =",
" BS.interact $ \\inputs ->",
" let ls = BS.lines inputs",
" n = readInt $ head ls",
" xs = map readInt . BS.words $ ls !! 1 :: [Int]",
" in BS.unwords (map (BS.pack . show) (solve xs)) <> BS.pack \"\\n\""
]
},
"Interact2DIntMatrix": {
"scope": "haskell",
"prefix": "interact2d",
"description": "Read a 2D matrix via interact with solve function.",
"body": [
"{-# OPTIONS_GHC -Wunused-imports #-}",
"{-# LANGUAGE BangPatterns #-}",
"{-# LANGUAGE CPP #-}",
"import Debug.Trace (traceShowId)",
"",
"-- {-# OPTIONS_GHC -DATCODER #-}",
"#ifdef ATCODER",
"debug :: Bool ; debug = False",
"#else",
"debug :: Bool ; debug = True",
"#endif",
"",
"dbgId :: (Show a) => a -> a",
"dbgId x",
" | debug = traceShowId x",
" | otherwise = x",
"",
"solve :: [[Int]] -> [[Int]]",
"solve xss = dbgId $ map (map (subtract 0)) xss",
"",
"main :: IO ()",
"main = interact $ \\inputs ->",
" let ls = lines inputs",
" [_, _] = map read . words $ head ls :: [Int]",
" intList2D = map (map read . words) $ drop 1 ls :: [[Int]]",
" in unlines . map (unwords . map show) $ solve intList2D"
]
},
"ArrowInteractPipeline": {
"scope": "haskell",
"prefix": "interactArrow",
"description": "Interact pipeline using Control.Arrow with solve function.",
"body": [
"{-# OPTIONS_GHC -Wunused-imports #-}",
"{-# LANGUAGE BangPatterns #-}",
"import Control.Arrow ((>>>))",
"",
"solve :: [Int] -> [Int]",
"solve xs = xs",
"",
"main :: IO ()",
"main =",
" interact $",
" words >>> map (read :: String -> Int) >>> solve >>> map show >>> unwords >>> (++ \"\\n\")"
]
},
"InteractWithSolve": {
"scope": "haskell",
"prefix": "interactSolve",
"description": "Interact with solve function template.",
"body": [
"{-# OPTIONS_GHC -Wunused-imports #-}",
"{-# LANGUAGE BangPatterns #-}",
"{-# LANGUAGE CPP #-}",
"import Debug.Trace (traceShowId)",
"",
"-- {-# OPTIONS_GHC -DATCODER #-}",
"#ifdef ATCODER",
"debug :: Bool ; debug = False",
"#else",
"debug :: Bool ; debug = True",
"#endif",
"",
"dbgId :: (Show a) => a -> a",
"dbgId x",
" | debug = traceShowId x",
" | otherwise = x",
"",
"solve :: [Int] -> [Int]",
"solve xs = dbgId $ map (subtract 0) xs",
"",
"main :: IO ()",
"main =",
" interact $ \\inputs ->",
" let ls = lines inputs",
" n = (read :: String -> Int) $ head ls",
" xs = map read . words $ ls !! 1 :: [Int]",
" in unwords (map show (solve xs)) ++ \"\\n\""
]
},
"BS.interact2d": {
"prefix": "BS.interact2d",
"body": [
"{-# OPTIONS_GHC -Wunused-imports #-}",
"{-# LANGUAGE BangPatterns #-}",
"{-# LANGUAGE CPP #-}",
"{-# LANGUAGE OverloadedStrings #-}",
"",
"-- {-# OPTIONS_GHC -DATCODER #-}",
"import Data.ByteString.Char8 qualified as BS",
"import Debug.Trace (traceShowId)",
"",
"#ifdef ATCODER",
"debug :: Bool ; debug = False",
"#else",
"debug :: Bool ; debug = True",
"#endif",
"",
"dbgId :: (Show a) => a -> a",
"dbgId x",
" | debug = traceShowId x",
" | otherwise = x",
"",
"-- ByteString版 read",
"readInt :: BS.ByteString -> Int",
"readInt bs =",
" case BS.readInt bs of",
" Just (x, _) -> x",
" Nothing -> error \"input is not integer\"",
"",
"solve :: [[Int]] -> [[Int]]",
"solve xss = dbgId $ map (map (subtract 0)) xss",
"",
"main :: IO ()",
"main = BS.interact $ \\inputs ->",
" let ls = BS.lines inputs",
" [_, _] = map readInt . BS.words $ head ls :: [Int]",
" intList2D = map (map readInt . BS.words) $ drop 1 ls :: [[Int]]",
" in BS.unlines . map (BS.unwords . map (BS.pack . show)) $ solve intList2D"
],
"description": "(BS version) Read a 2D matrix via interact with solve function"
}
}