Skip to content

Instantly share code, notes, and snippets.

@haylinmoore
Last active August 27, 2022 22:09
Show Gist options
  • Save haylinmoore/5f4a3b6818baf9fcf2113ef73eac2e06 to your computer and use it in GitHub Desktop.
Save haylinmoore/5f4a3b6818baf9fcf2113ef73eac2e06 to your computer and use it in GitHub Desktop.
let data = [{"Name": "John Doe", "Grade": 95 }, {"Name": "Mary Jane", "Grade": 98 }, {"Name": "Alice Templeton", "Grade": 90 }]
let sum = List/fold { cons: \x y -> x + y : Natural, nil: 0 }
let len = \d -> List/length d
let introduce = \person -> "Hello, "+person."Name" + " how are you doing, you got a "
let replicate
: forall (a : Type) . Natural -> a -> List a
= \n -> \x -> Natural/fold n (\xs -> [ x ] + xs) []
let generate
: forall (a : Type) . Natural -> (Natural -> a) -> List a
= \n -> \f -> List/map (\x -> f x.index) (List/indexed (replicate n { }))
let divide:
Natural -> Natural -> Natural -> Real -> Real
= \x -> \y -> \s -> \i ->
let xb = x*s
# Get a list of numbers 0, y, 2*y, y*3, ... y*xb
let l = generate xb (\xs -> (xs)*y) : List Natural
# Filter list so any numbers bigger than x are 0
let l = List/map (\v ->
let d = v : Natural
let ret = if Real/lessThan v xb
then d
else if Real/equal v xb
then d
else 0
in ret
) l : List Natural
# Count numbers greater than 0
let d = List/fold { cons: \next -> \acc ->
let next = next : Natural
in if Real/equal next 0
then acc
else (acc + i)
, nil: 0 } (l)
in (d)
let forceValue = merge { Some: \n -> n, None: \_ -> "" }
let textFold = \t s ->
let last = forceValue (List/last t)
let nmo = List/reverse (List/drop 1 (List/reverse t))
in (List/fold {cons: \x -> \y -> x + s + y:Text, nil: last } nmo)
let res = List/map (\student -> introduce student) data
let res = res
in {
"Student Names": textFold (List/map (\student -> student."Name") data) ", ",
"Student Total": sum (List/map (\student -> student."Grade") data),
"Student Count": len data,
"Student Avg": divide (sum (List/map (\student -> student."Grade" : Natural) data)) (len data) 100 0.01,
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment