Skip to content

Instantly share code, notes, and snippets.

@darkf darkf/idk.py
Created Nov 19, 2016

Embed
What would you like to do?
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
You can’t perform that action at this time.