Skip to content

Instantly share code, notes, and snippets.

@lachezar
Created October 5, 2019 19:21
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save lachezar/45bb8168447779c7ab24f69b8d4edb07 to your computer and use it in GitHub Desktop.
Save lachezar/45bb8168447779c7ab24f69b8d4edb07 to your computer and use it in GitHub Desktop.
Words in Haskell vs Typescript
{-# 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
// 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