Skip to content

Instantly share code, notes, and snippets.

@ssanj
ssanj / Parser.hs
Last active November 16, 2021 10:37
module MyParser where
import Text.Read (readMaybe)
newtype Parser a = Parser { runParser :: String -> Either String (a, String) }
parseChar :: Char -> Parser Char
parseChar char =
Parser $ \input ->
case input of
{-# LANGUAGE OverloadedStrings #-}
{-# LANGUAGE QuasiQuotes #-}
{-# LANGUAGE ScopedTypeVariables #-}
module ExampleErrorSpec where
import Test.Hspec
import Test.Hspec.Wai
spec :: Spec
import fs2.Stream
import fs2.Pipe
import fs2.Chunk
import fs2.Pull
import cats.effect.IO
import scala.language.higherKinds
//implementing scan from https://fs2.io/guide.html#exercises-stream-transforming
//Stream.range(1,10).scan(0)(_ + _).toList // running sum
// res38: List[Int] = List(0, 1, 3, 6, 10, 15, 21, 28, 36, 45)
//I'm trying to implement `intersperse` from : https://fs2.io/guide.html#exercises-stream-transforming
import fs2.Stream
import fs2.Pipe
import fs2.Chunk
import fs2.Pull
import cats.effect.IO
import scala.language.higherKinds
object Intersperse {
@ssanj
ssanj / count.hs
Last active January 7, 2020 13:07
count :: Int -> IO Int
count it
| it <= 0 = pure 0
| otherwise =
do
n <- count (it - 1)
pure (n + 1)
scala> trait A { val a: String = "this is A" }
defined trait A
scala> trait B { val b: String = "this is B" }
defined trait B
scala> trait C { val c: String = "this is C" }
defined trait C
scala> val abc = new A with B with C
type ErrorOr[A] = Either[String, A]
def errors(error: String): () => ErrorOr[Int] = () => {
println(s"running $error")
Left(error)
}
scala> val item1 = errors("error1")
item1: () => ErrorOr[Int] = $$Lambda$4361/1034150118@23f4a773
@ssanj
ssanj / pretty-json.js
Created October 20, 2019 12:46
Pretty Print Json in Haskell
{-# LANGUAGE OverloadedStrings #-}
module Milo.Blog where
import qualified Data.Text as T
import qualified Data.Text.IO as TIO
import qualified Data.Text.Encoding as E
import qualified Data.ByteString.Lazy as LBS
import qualified Data.ByteString.Lazy.Char8 as LBS8
import qualified Data.Aeson as A (Value(String))