Skip to content

Instantly share code, notes, and snippets.

libraryDependencies += "com.chuusai" % "shapeless_2.11" % "2.3.2"
val list1 : List[Int] = List(1,2,3) // list of integers
val list2 : List[String] = List("a","b", "c") // list of strings
val list3 : List[Any] = List(1,2,"a","b") // list of any. Here Any is the least upper bound of sting & int
import shapeless._
val hlist1a = HList(1,2,3)
val hlist2a = HList(1,"a",3.14)
// The above code with type ascription
val hlist1b: ::[Int, ::[Int, ::[Int, HNil]]] = HList(1,2,3)
val hlist2b: ::[Int, ::[String, ::[Double, HNil]]] = HList(1,"a",3.15)
val l = 1 :: "hello" :: true :: HNil
// l: shapeless.::[Int,shapeless.::[String,shapeless.::[Boolean,shapeless.HNil]]] = 1 :: hello :: true :: HNil
l.head
// res7: Int = 1
l.tail
// res8: shapeless.::[String,shapeless.::[Boolean,shapeless.HNil]] = hello :: true :: HNil
l(1)
@hardvain
hardvain / Evaluator.idr
Last active November 5, 2017 07:59
[Gaining a Monad Intuition] Learn why the monad design pattern came into existence #monads #functional-programming #haskell
module IParsec
interface Source a where
feed : Nat -> a -> (List Char, a)
next : a -> (List Char, a)
next = feed 1
Source String where
feed x string = let (first, second) = splitAt x $ unpack string in
(first, pack second)
@hardvain
hardvain / json.hs
Created January 11, 2018 07:48 — forked from fero23/json.hs
JSON Parser with Haskell's Parsec
import Text.ParserCombinators.Parsec
import Data.List
type Args = [String]
type Body = [String]
type Label = String
data JSONProp = JSONProp Label JSON deriving Show
data JSON = JSONObject [JSONProp]
| JSONNumber Double
@hardvain
hardvain / JSONParser.hs
Created January 11, 2018 07:48 — forked from zearen/JSONParser.hs
A simple haskell demonstation showing parsing JSON with Parsec
{-
Zachary Weaver <zaw6@pitt.edu>
JSONParser.hs
Version 0.1.1
A simple example of parsing JSON with Parsec in haskell. Note that
since the primary point of this excersize is demonstration,
Text.Parsec.Token was avoided to expose more of the grammar. Also,
complicated optimizations and shorcuts were avoided (mostly).
@hardvain
hardvain / list.rs
Created January 30, 2018 16:02
Singly Linked List in Rust
use std::fmt::*;
/// A struct that represents a single node in a list
///
/// # State
/// * `element` - The element of type T that is stored in the node
/// * `next` - An optional value that points to the next element in the list
#[derive(PartialEq, Debug)]
pub struct Node<T: Debug> {
pub element: T,
@hardvain
hardvain / Error
Last active August 12, 2018 06:37
SVG
• Couldn't match type ‘Circle’ with ‘Line’
Expected type: Tree Line
Actual type: Tree Circle
• In the expression: Node (Circle (20, 30) 50) []
In the second argument of ‘Node’, namely
‘[Node (Circle (20, 30) 50) []]’
In the expression:
Node (Line (100, 100) (30, 60)) [Node (Circle (20, 30) 50) []]
|
11 | sampleTree = Node (Line (100,100) (30,60)) [Node (Circle (20,30) 50) []]
@hardvain
hardvain / SVG.hs
Created August 13, 2018 14:11
SVG
{-# LANGUAGE ExistentialQuantification, GADTs #-}
module Main where
class SvgNode a where
toSVG :: a -> String
data SvgNodeObject where
Pack :: SvgNode a => a -> SvgNodeObject