Skip to content

Commit 8e2e05c

Browse files
committed
Update consolidated snippets
1 parent 12d2ae6 commit 8e2e05c

File tree

2 files changed

+307
-0
lines changed

2 files changed

+307
-0
lines changed

public/consolidated/_index.json

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,10 @@
1515
"lang": "CSS",
1616
"icon": "/icons/css.svg"
1717
},
18+
{
19+
"lang": "HASKELL",
20+
"icon": "/icons/haskell.svg"
21+
},
1822
{
1923
"lang": "HTML",
2024
"icon": "/icons/html.svg"

public/consolidated/haskell.json

Lines changed: 303 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,303 @@
1+
[
2+
{
3+
"categoryName": "Array Manipulation",
4+
"snippets": [
5+
{
6+
"title": "Binary Search",
7+
"description": "Searches for an element in a sorted array using binary search.",
8+
"author": "ACR1209",
9+
"tags": [
10+
"haskell",
11+
"array",
12+
"binary-search",
13+
"search"
14+
],
15+
"contributors": [],
16+
"code": "binarySearch :: Ord a => a -> [a] -> Maybe Int\nbinarySearch _ [] = Nothing\nbinarySearch target xs = go 0 (length xs - 1)\n where\n go low high\n | low > high = Nothing\n | midElem < target = go (mid + 1) high\n | midElem > target = go low (mid - 1)\n | otherwise = Just mid\n where\n mid = (low + high) `div` 2\n midElem = xs !! mid\n\nmain :: IO ()\nmain = do\n let array = [1, 2, 3, 4, 5]\n print $ binarySearch 3 array -- Output: Just 2\n print $ binarySearch 6 array -- Output: Nothing\n"
17+
},
18+
{
19+
"title": "Chunk Array",
20+
"description": "Splits an array into chunks of a specified size.",
21+
"author": "ACR1209",
22+
"tags": [
23+
"haskell",
24+
"array",
25+
"chunk",
26+
"utility"
27+
],
28+
"contributors": [],
29+
"code": "chunkArray :: Int -> [a] -> [[a]]\nchunkArray _ [] = []\nchunkArray n xs = take n xs : chunkArray n (drop n xs)\n\nmain :: IO ()\nmain = do\n let array = [1, 2, 3, 4, 5, 6]\n print $ chunkArray 2 array -- Output: [[1, 2], [3, 4], [5, 6]]\n"
30+
},
31+
{
32+
"title": "Flatten Array",
33+
"description": "Flattens a multi-dimensional array.",
34+
"author": "ACR1209",
35+
"tags": [
36+
"haskell",
37+
"array",
38+
"flatten",
39+
"utility"
40+
],
41+
"contributors": [],
42+
"code": "flatten :: [[a]] -> [a]\nflatten = concat\n\nmain :: IO ()\nmain = do\n let array = [[1, 2], [2], [3], [4]]\n print $ flatten array -- Output: [1, 2, 2, 3, 4]\n"
43+
},
44+
{
45+
"title": "Matrix Transpose",
46+
"description": "Transposes a 2D matrix.",
47+
"author": "ACR1209",
48+
"tags": [
49+
"haskell",
50+
"array",
51+
"matrix",
52+
"transpose"
53+
],
54+
"contributors": [],
55+
"code": "transposeMatrix :: [[a]] -> [[a]]\ntransposeMatrix [] = []\ntransposeMatrix ([]:_) = []\ntransposeMatrix xs = map head xs : transposeMatrix (map tail xs)\n\nmain :: IO ()\nmain = do\n let matrix = [[1, 2, 3], [4, 5, 6], [7, 8, 9]]\n print $ transposeMatrix matrix -- Output: [[1,4,7],[2,5,8],[3,6,9]]\n"
56+
},
57+
{
58+
"title": "Remove duplicates",
59+
"description": "Removes duplicate values from an array.",
60+
"author": "ACR1209",
61+
"tags": [
62+
"haskell",
63+
"array",
64+
"deduplicate",
65+
"utility"
66+
],
67+
"contributors": [],
68+
"code": "import Data.List (nub)\n\nremoveDuplicates :: Eq a => [a] -> [a]\nremoveDuplicates = nub\n\n-- Usage\nmain :: IO ()\nmain = do\n let array = [1, 2, 2, 3, 4, 4, 5]\n print $ removeDuplicates array -- Output: [1, 2, 3, 4, 5]\n"
69+
}
70+
]
71+
},
72+
{
73+
"categoryName": "Basics",
74+
"snippets": [
75+
{
76+
"title": "Hello, World!",
77+
"description": "Prints Hello, World! to the terminal.",
78+
"author": "ACR1209",
79+
"tags": [
80+
"haskell",
81+
"printing",
82+
"hello-world",
83+
"utility"
84+
],
85+
"contributors": [],
86+
"code": "putStrLn \"Hello, World!\"\n"
87+
}
88+
]
89+
},
90+
{
91+
"categoryName": "File Handling",
92+
"snippets": [
93+
{
94+
"title": "Append to File",
95+
"description": "Appends text to an existing file.",
96+
"author": "ACR1209",
97+
"tags": [
98+
"haskell",
99+
"file",
100+
"append",
101+
"utilty"
102+
],
103+
"contributors": [],
104+
"code": "import System.IO\n\nappendToFile :: FilePath -> String -> IO ()\nappendToFile = appendFile \n\nmain :: IO ()\nmain = do\n let file = \"example.txt\"\n let text = \"This will be appended to the file.\\n\"\n appendToFile file text\n"
105+
},
106+
{
107+
"title": "Check if File Exists",
108+
"description": "Checks if a file exists at a given path.",
109+
"author": "ACR1209",
110+
"tags": [
111+
"haskell",
112+
"file",
113+
"exists"
114+
],
115+
"contributors": [],
116+
"code": "import System.Directory (doesFileExist)\n\ncheckFileExists :: FilePath -> IO Bool\ncheckFileExists = doesFileExist\n\nmain :: IO ()\nmain = do\n let file = \"example.txt\"\n exists <- checkFileExists file\n if exists then putStrLn \"File exists.\" else putStrLn \"File does not exist.\"\n"
117+
},
118+
{
119+
"title": "Find Files in Directory by Type",
120+
"description": "Finds all files in a directory with a specific extension.",
121+
"author": "ACR1209",
122+
"tags": [
123+
"haskell",
124+
"file",
125+
"search",
126+
"extension",
127+
"filesystem"
128+
],
129+
"contributors": [],
130+
"code": "import System.Directory (listDirectory)\nimport System.FilePath (takeExtension)\n\nfindFilesByExtension :: FilePath -> String -> IO [FilePath]\nfindFilesByExtension dir ext = do\n files <- listDirectory dir\n return $ filter (\\f -> takeExtension f == ext) files\n\nmain :: IO ()\nmain = do\n let directory = \".\"\n let ext = \".txt\"\n files <- findFilesByExtension directory ext\n mapM_ putStrLn files -- Output: list of txt files on the current directory\n"
131+
},
132+
{
133+
"title": "Read File in Chunks",
134+
"description": "Reads a file in chunks grouped by lines.",
135+
"author": "ACR1209",
136+
"tags": [
137+
"haskell",
138+
"file",
139+
"read",
140+
"chunks",
141+
"utility"
142+
],
143+
"contributors": [],
144+
"code": "import System.IO (openFile, IOMode(ReadMode), hGetContents)\nimport Data.List (unfoldr)\n\nreadFileInChunks :: FilePath -> Int -> IO [[String]]\nreadFileInChunks filePath chunkSize = do\n handle <- openFile filePath ReadMode\n contents <- hGetContents handle\n let linesList = lines contents\n return $ go linesList\n where\n go [] = []\n go xs = take chunkSize xs : go (drop chunkSize xs)\n\nmain :: IO ()\nmain = do\n let file = \"example.txt\"\n let chunkSize = 3 -- Number of lines per chunk\n chunks <- readFileInChunks file chunkSize\n mapM_ (putStrLn . unlines) chunks\n\n"
145+
},
146+
{
147+
"title": "Write to File",
148+
"description": "Writes text to a file, overwriting any existing content.",
149+
"author": "ACR1209",
150+
"tags": [
151+
"haskell",
152+
"file",
153+
"write"
154+
],
155+
"contributors": [],
156+
"code": "import System.IO (writeFile)\n\nwriteToFile :: FilePath -> String -> IO ()\nwriteToFile = writeFile\n\nmain :: IO ()\nmain = do\n let file = \"example.txt\"\n let content = \"This is new content.\"\n writeToFile file content\n"
157+
}
158+
]
159+
},
160+
{
161+
"categoryName": "Monads",
162+
"snippets": [
163+
{
164+
"title": "Either Monad for Error Handling",
165+
"description": "Using the Either monad to handle errors in a computation.",
166+
"author": "ACR1209",
167+
"tags": [
168+
"haskell",
169+
"monads",
170+
"either",
171+
"error handling"
172+
],
173+
"contributors": [],
174+
"code": "safeDiv :: Int -> Int -> Either String Int\nsafeDiv _ 0 = Left \"Division by zero error\"\nsafeDiv x y = Right (x `div` y)\n\nmain :: IO ()\nmain = do\n let result = do\n a <- safeDiv 10 2\n b <- safeDiv a 0 -- This will trigger an error\n return b\n print result -- Output: Left \"Division by zero error\"\n"
175+
},
176+
{
177+
"title": "Maybe Monad",
178+
"description": "Using the Maybe monad to handle computations that might fail.",
179+
"author": "ACR1209",
180+
"tags": [
181+
"haskell",
182+
"monads",
183+
"maybe"
184+
],
185+
"contributors": [],
186+
"code": "safeDiv :: Int -> Int -> Maybe Int\nsafeDiv _ 0 = Nothing\nsafeDiv x y = Just (x `div` y)\n\nmain :: IO ()\nmain = do\n let result = do\n a <- safeDiv 10 2\n b <- safeDiv a 2\n return b\n print result -- Output: Just 2\n"
187+
},
188+
{
189+
"title": "State Monad",
190+
"description": "Managing mutable state using the State monad.",
191+
"author": "ACR1209",
192+
"tags": [
193+
"haskell",
194+
"monads",
195+
"state",
196+
"state-management"
197+
],
198+
"contributors": [],
199+
"code": "import Control.Monad.State\n\nincrement :: State Int Int\nincrement = do\n count <- get\n put (count + 1)\n return count\n\nmain :: IO ()\nmain = do\n let (res1, intermediateState) = runState increment 0\n print res1 -- Output: 0\n let (result, finalState) = runState increment intermediateState\n print result -- Output: 1\n print finalState -- Output: 2\n\n"
200+
},
201+
{
202+
"title": "Writer Monad",
203+
"description": "Using the Writer monad to accumulate logs or other outputs alongside a computation.",
204+
"author": "ACR1209",
205+
"tags": [
206+
"haskell",
207+
"monads",
208+
"writer",
209+
"logs"
210+
],
211+
"contributors": [],
212+
"code": "import Control.Monad.Writer\n\naddAndLog :: Int -> Int -> Writer [String] Int\naddAndLog x y = do\n tell [\"Adding \" ++ show x ++ \" and \" ++ show y]\n return (x + y)\n\nmain :: IO ()\nmain = do\n let (result, logs) = runWriter $ do\n res1 <- addAndLog 3 5\n addAndLog res1 1\n print result -- Output: 9\n print logs -- Output: [\"Adding 3 and 5\", \"Adding 8 and 1\"]\n"
213+
}
214+
]
215+
},
216+
{
217+
"categoryName": "String Manipulation",
218+
"snippets": [
219+
{
220+
"title": "Transform Camel Case to Snake Case",
221+
"description": "Converts a Camel Case string to Snake case.",
222+
"author": "ACR1209",
223+
"tags": [
224+
"haskell",
225+
"string",
226+
"convert",
227+
"camel-case",
228+
"snake-case",
229+
"utility"
230+
],
231+
"contributors": [],
232+
"code": "import Data.Char (isUpper, toLower)\n\ncamelToSnake :: String -> String\ncamelToSnake [] = []\ncamelToSnake (x:xs)\n | isUpper x = '_' : toLower x : camelToSnake xs\n | otherwise = x : camelToSnake xs\n\nmain :: IO ()\nmain = do\n let camelCase = \"camelCaseToSnakeCase\"\n print $ camelToSnake camelCase -- Output: \"camel_case_to_snake_case\"\n"
233+
},
234+
{
235+
"title": "Capitalize Words",
236+
"description": "Capitalizes the first letter of each word in a string.",
237+
"author": "ACR1209",
238+
"tags": [
239+
"haskell",
240+
"string",
241+
"capitalize",
242+
"words"
243+
],
244+
"contributors": [],
245+
"code": "import Data.Char (toUpper)\n\ncapitalizeWords :: String -> String\ncapitalizeWords = unwords . map capitalize . words\n where\n capitalize [] = []\n capitalize (x:xs) = toUpper x : xs\n\nmain :: IO ()\nmain = do\n let sentence = \"haskell is awesome\"\n print $ capitalizeWords sentence -- Output: \"Haskell Is Awesome\"\n"
246+
},
247+
{
248+
"title": "Count Word Occurrences in String",
249+
"description": "Counts the occurrences of each word in a given string.",
250+
"author": "ACR1209",
251+
"tags": [
252+
"haskell",
253+
"string",
254+
"occurrences",
255+
"word-count"
256+
],
257+
"contributors": [],
258+
"code": "import Data.List (group, sort)\n\ncountWordOccurrences :: String -> [(String, Int)]\ncountWordOccurrences = map (\\(w:ws) -> (w, length (w:ws))) . group . sort . words\n\nmain :: IO ()\nmain = do\n let text = \"haskell is awesome and haskell is fun\"\n print $ countWordOccurrences text -- Output: [(\"and\",1),(\"awesome\",1),(\"fun\",1),(\"haskell\",2),(\"is\",2)]\n"
259+
},
260+
{
261+
"title": "Remove Punctuation",
262+
"description": "Removes all punctuation from a given string.",
263+
"author": "ACR1209",
264+
"tags": [
265+
"haskell",
266+
"string",
267+
"punctuation",
268+
"remove"
269+
],
270+
"contributors": [],
271+
"code": "import Data.Char (isPunctuation)\n\nremovePunctuation :: String -> String\nremovePunctuation = filter (not . isPunctuation)\n\nmain :: IO ()\nmain = do\n let text = \"Hello, Haskell! How's it going?\"\n print $ removePunctuation text -- Output: \"Hello Haskell Hows it going\"\n"
272+
},
273+
{
274+
"title": "Transform from Snake Case to Camel Case",
275+
"description": "Converts a Snake Case string to Camel Case.",
276+
"author": "ACR1209",
277+
"tags": [
278+
"haskell",
279+
"string",
280+
"convert",
281+
"snake-case",
282+
"camel-case",
283+
"utilty"
284+
],
285+
"contributors": [],
286+
"code": "import Data.Char (toUpper)\n\nsnakeToCamel :: String -> String\nsnakeToCamel [] = []\nsnakeToCamel ('_':x:xs) = toUpper x : snakeToCamel xs\nsnakeToCamel (x:xs) = x : snakeToCamel xs\n\nmain :: IO ()\nmain = do\n let snakeCase = \"snake_case_to_camel_case\"\n print $ snakeToCamel snakeCase -- Output: \"snakeCaseToCamelCase\"\n"
287+
},
288+
{
289+
"title": "Truncate Strings",
290+
"description": "Truncates a string to a specified length, optionally adding an ellipsis.",
291+
"author": "ACR1209",
292+
"tags": [
293+
"haskell",
294+
"string",
295+
"truncate",
296+
"utility"
297+
],
298+
"contributors": [],
299+
"code": "truncateString :: Int -> String -> String\ntruncateString maxLength str\n | length str <= maxLength = str\n | otherwise = take (maxLength - 3) str ++ \"...\"\n\nmain :: IO ()\nmain = do\n let longString = \"Haskell is a powerful functional programming language.\"\n print $ truncateString 20 longString -- Output: \"Haskell is a powe...\"\n print $ truncateString 54 longString -- Output: \"Haskell is a powerful functional programming language.\"\n"
300+
}
301+
]
302+
}
303+
]

0 commit comments

Comments
 (0)