Created
November 19, 2016 04:33
-
-
Save darkf/298d35bb4d5919ffd8962c5949437e16 to your computer and use it in GitHub Desktop.
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
import random | |
# types are first class | |
List :: Type | |
List[Int] :: Type # just List indexed by another type ;) | |
xs = [1, 2, 3] # inferred type: xs :: List[Int] | |
for(xs, print) | |
# equivalent to: | |
for(xs, x -> | |
print(x)) | |
# iteration: | |
# the "callback" is overloaded, as such | |
# it can take a continuation for breaking early, imperative style: | |
for([1..], (x, break) -> | |
if x > 3: break() else: print(x)) | |
# types | |
type Point | |
x :: Num | |
y :: Num | |
# (cross-module) multimethods | |
def +({x,y} :: Point, {a,b} :: Point): | |
Point {x+a, y+b} | |
Point {1, 2} + Point {3, 4} == Point {4, 6} | |
# monads, Just Cause | |
type Maybe[T] | |
Just :: T | Nothing :: Any | |
def >>=(m :: Maybe[T], f :: (T -> Maybe[T])): | |
if m.Just != nil: f(m.Just) | |
else: Nothing | |
# unit is a problem unless we also match based on the return type, | |
# which then allows us: | |
def unit(x :: T) -> Maybe[T]: Just(x) | |
# could be image-based and the interpreter would support (de)serialization | |
# as a first-class thing |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment