Skip to content

Instantly share code, notes, and snippets.

View CMCDragonkai's full-sized avatar
🚀
Lightspeed

CMCDragonkai

🚀
Lightspeed
View GitHub Profile
@CMCDragonkai
CMCDragonkai / y_combinator.exs
Created November 14, 2014 04:47
Elixir: Y (Fixed Point) Combinator
# Y = \f.(\x.f(x x))(\x.f(x x))
# Y = \f.f(x x) [x := \x.f(x x)]
# Y = \f.f((\x.f(x x)(\x.f(x x))
#
# Y f = f (Y f)... recurses
# Y combinator
fix = fn f ->
# apply the second expression to itself
(fn z ->
@CMCDragonkai
CMCDragonkai / list_of.js
Last active August 29, 2015 14:09
OMetaJS: List Of Higher Order
ometa List {
listOf :p = apply(p):head (',' apply(p))*:tail -> [head].concat(tail),
listOfDigits = listOf('digit'):l -> l,
END
}
var result = List.matchAll(
'1,2,3,4,5',
'listOfDigits'
);
@CMCDragonkai
CMCDragonkai / repeat.js
Last active August 29, 2015 14:09
OMetaJS: Repeat Higher Order
ometa Test {
repeat :n :p = ?(n == 0) -> ('') // base case
| apply(p):l repeat(n - 1, p):r -> ((r.length > 0) ? [l].concat(r) : [l]), // concat the recursive matches
tom = "TOM",
repeatTOM = repeat(3, 'tom')
}
Test.matchAll(
'TOMTOMTOM',
'repeatTOM'
@CMCDragonkai
CMCDragonkai / lines.js
Last active August 29, 2015 14:09
OMetaJS: Lines. This allows you to match the first line, empty lines and the last line. Do not try to recursively match empty, end, or empty strings. This will result in infinite recursion. Because this string `""` contains an infinite amount of empty strings.
ometa Lines {
line = '\n'
| (~'\n' string)+:l '\n'?:n -> ((n != null) ? l.join("") + n : l.join("")),
lines = line*:l end -> (l)
}
var input =
"first\
\
\
@CMCDragonkai
CMCDragonkai / eof.js
Last active August 29, 2015 14:09
OMetaJS: End of File, put the EOF rule at the end of any top level rule. It forces it to match the entire file. So you can't have partial syntax errors. Without the EOF, OMeta would only parse until it meets something it cannot understand, resulting in a partial parse.
ometa EndOfFile {
EOF = spaces end,
top = "something":s EOF -> (s)
}
EndOfFile.matchAll(
"something",
"top"
);
@CMCDragonkai
CMCDragonkai / string_matching.md
Last active August 29, 2015 14:09
OMetaJS: String Matching Syntax

OMetaJS String Matching Syntax

OMeta can parse more than just text streams. It can operate on typed objects that the host language understands. That's why we have different string matching syntax for different purposes.

  • #abc or ``abc` => match the string object 'abc'
  • 'abc' => match the string object 'abc'
  • 'c' => match the string object 'c'
  • ```abc''orseq("abc")` => match the sequence of string objects 'a', 'b', 'c'
  • "abc" => match token('abc')
@CMCDragonkai
CMCDragonkai / markdown.js
Last active May 27, 2020 17:02
OMetaJS: Simple Markdown
var text =
"hi\
=== \
### hi hi\
another\
--------\
\
hello\
\
\
@CMCDragonkai
CMCDragonkai / compile.sh
Created December 13, 2014 08:30
Mercury: Shell script for automating the organisation of compiling Mercury programs. The current compiler for Mercury puts all the build artifacts into the current working directory which is very messy.
#!/usr/bin/env bash
# build the mercury programs!
# development artifacts are in build
# release artifacts are in release
mkdir -p build
mkdir -p release
program=$@
@CMCDragonkai
CMCDragonkai / bulk_update.sql
Last active August 29, 2015 14:15
SQL: Bulk Update (with Heterogeneous Rows)
-- Switch Case Update (MySQL)
UPDATE table
SET field1 = CASE id
WHEN 1 THEN 'value1'
WHEN 2 THEN 'value2'
WHEN 3 THEN 'value3'
ELSE 'value4'
END,
field2 = CASE id
@CMCDragonkai
CMCDragonkai / env.md
Last active February 7, 2018 15:26
CLI: Setting & Unsetting Environment Variables on Linux & Windows

Windows

Show environment variable:

CMD

echo %ENVIRONMENT_VARIABLE%