Skip to content

Instantly share code, notes, and snippets.

@HerringtonDarkholme
Last active June 3, 2023 20:31
Show Gist options
  • Save HerringtonDarkholme/5e2d978840f6c1bb737b854b0f78eac6 to your computer and use it in GitHub Desktop.
Save HerringtonDarkholme/5e2d978840f6c1bb737b854b0f78eac6 to your computer and use it in GitHub Desktop.
# I am comment
# Basic Data type
123 # int
12.3 # float
6.0221409e+23 # scentific notation
true # bool
"string"
# String Operation
string: "some string"
multiline: """
multiple line
"""
escaped: "escape using \\, \", \{\}"
# List
list:
* true # must have no comma
* "2"
* 3
oneline: [1, 2, 3, 4,]
indentationIgnorantList: [
1, # must have comma
2,
3,
]
# Dictionary
object:
key: value
key:
nested: value
moreValue: 42
nested: path: in: object: 42
[123]: "number key!"
onelineObj: {a: 1, b: 2, c: 3}
indentationIgnorantList: {
a: 1, # must have comma
key:
you: "can" # shall we allow this?
mix: "two",
[true]: "boolean key",
}
# set?
let numSet = set(1,2,3,4)
let setLiteral = %[1,2,3,4] # what about sorted set?
# tuple?
let numTuple = tuple(1,2,3,4)
let tupleLiteral = (1,2,3,4)
# we can import something
import m = "./simple"
# Binding
let varname = 123
let funcAdd(a: Int, b: Int) = a + b
let annotated: int = 123 # type annotation is supported
# String Operation
let interpolation = "Hello {name}"
let escaped = "escape using \\, \", \{\}"
# List
let spreaded = [
...spread # spread requires clear context
]
let multiLineSpread =
* 1
* 2
...spread # clear context
let comprehension = [
num * 810
for item in list
if item
]
# Dictionary
let object =
key: value
key:
nested: value
moreValue: 42
nested: path: in: object: 42
[dynamicKey]: dynamicValue
...spread
let objComprehension = {
[key]: value
for key, value in
keyedContainer
}
let shortHand = {
x, y, z,
...spread,
[keyed]: 123,
nested: path: in: object: 42,
}
let deepMerge = object ^ shortHand
# access object
let a = shortHand.x
let b = shortHand.y
# function literal
let func = \a b c ->
let aSquare = a * a
let bSquare = b * b
let cSquare = c * c
aSquare + bSquare + cSquare
let funcAnnotation = \(a: Int, b: Int) ->
a + b
let funcDestructuring = \({a, b}: Struct, [e, tail]: List(Int)) ->
ree
let shorthandParam = \$0.name
let applied = func(1, 2, 3) # 1 + 4 + 9 = 14
# combine together!
* numField: 123
field: nested: inner: 123
nested:
test
* func: \a b c d ->
test
num: 123
* test
func: \ a b c d ->
wewe
nested: nested: nested:
* 1
* 2
* 3
# set?
let numSet = set(1,2,3,4)
let setLiteral = %[1,2,3,4] # what about sorted set?
# tuple?
let numTuple = tuple(1,2,3,4)
let tupleLiteral = (1,2,3,4)
# conditional
let ifElse =
if a then b else c
# pattern matching
let matchExpr = match test
String -> 'String'
b @ Int -> 123 + s # pattern binding
Struct {age} -> age
Bool ->
nested: value
also: works
(first, second) -> 'tuple works'
else 123
# Types
type UserID = Int
type List(A: Type) =
| Nil
| Head: A
Tail: List(A)
# type operator
type StringOrNum = String | Int
type Duck = Swimmable & Flyable & Walkable
type MultiLineDuck =
& Swimmable
& Flyable
& Walkable
type Struct =
firstName: String
lastName: String
age: Int
type IntList = List(Int)
# type in diana is constraint: what kind of value does a type accept
# we can merge two type by combining two constraints
type Natural = Int & > 0
type IsAdult = Natural & > 18
type Adult = Person & { age: IsAdult }
type Merge = Conflict ^ Conflict2 # prefer latter
# a value is a constraint which only accepts the value itself
type Singleton = 3
type UnificationError = 3 & 4 # impossible
kind Monad(Container: Type -> Type) = ???
# diana structure
# import statements
# let statements
# last export expression
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment