Created
October 5, 2019 19:21
-
-
Save lachezar/45bb8168447779c7ab24f69b8d4edb07 to your computer and use it in GitHub Desktop.
Words in Haskell vs Typescript
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{-# LANGUAGE FlexibleContexts #-} | |
{-# LANGUAGE QuasiQuotes #-} | |
module Lib | |
( run | |
) where | |
import Text.Regex.PCRE.Heavy | |
newtype Word' = | |
Word' String | |
instance Show Word' where | |
show (Word' s) = s | |
wordsFromString :: String -> [Word'] | |
wordsFromString text = [Word' w | w <- split [re|\W+|] text, w /= ""] | |
printWords :: [Word'] -> IO () | |
printWords words = putStrLn $ unwords $ show <$> words |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// a word does not contain white space in itself | |
type Word = { | |
kind: "word"; | |
value: string; | |
} | |
const wordsFromString = (s: String): Word[] => { | |
return s.split(/\W+/).filter((w) => w != "").map((w) => { return { value: w, kind: "word" } }); | |
} | |
const printWords = (words: Word[]): void => { | |
console.log("Words: ", words.map(w => w.value).join(" ")); | |
} | |
const text = "q w e r t y "; | |
printWords(wordsFromString(text)); | |
printWords([{ value: "q", kind: "word" }, { value: "w", kind: "word" }]); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment