Skip to content

Instantly share code, notes, and snippets.

function balance {
['black', ['red', ['red', *, *, *], *, *], *, *] => 'balance-1',
['black', ['red', *, *, ['red', *, *, *]], *, *] => 'balance-2',
['black', *, *, ['red', ['red', *, *, *], *, *]] => 'balance-3',
['black', *, *, ['red', *, *, ['red', *, *, *]]] => 'balance-4',
* => 'balanced'
}
function test1 {
Black(Red(Red(*, *, *), *, *), *, *) => 'balance-1',
Black(Red(*, *, Red(*, *, *)), *, *) => 'balance-2',
Black(*, *, Red(Red(*, *, *), *, *)) => 'balance-3',
Black(*, *, Red(*, *, Red(*, *, *))) => 'balance-4',
default => 'balanced'
}
function test2(tree) {
var res;
macro to_str {
case { _ ($toks ...) } => {
return [makeValue(#{ $toks ... }.map(unwrapSyntax).join(''), #{ here })];
}
}
to_str(1 2 3 4)
// '1234'
macro protocol {
case { _ $name:ident { $fn:protocol_fn ... } } => {
// Probably not a good idea to rely on `__fresh`. Sweet.js should provide
// some sort of blessed gensym capability.
letstx $id = [makeValue(__fresh(), #{ here })];
return #{
function $name(proto, impl) {
if (!impl) {
impl = proto;
proto = {};
module Control.Global (importLib) where
foreign import importLib
"function importLib(node) {\
\ return function(global) {\
\ if (typeof require !== 'undefined' && typeof process !== 'undefined') {\
\ return require(node);\
\ } else {\
\ return window[global];\
\ }\
test = map ((*) 2) >>> filter ((>) 15) >>> drop 3 >>> map show
src1 = [10, 9, 8, 7, 6, 5, 4, 3, 2, 1]
src2 = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
res1 = transduce' test src1 :: [String]
res2 = transduce' test src2 :: List String
macro constexpr {
rule { ($e:expr) } => {
(function() {
macro cexpr {
case { _ } => {
return [makeValue($e, #{ here })];
}
}
return cexpr;
}())
@natefaubion
natefaubion / thread-result.js
Last active January 5, 2017 23:58
Sweet.js threading macro
arg |> foo[0].bar(42)
|> baz("hello", "there")
|> quux.foo().bar()
|> new Foo()
// Expands to:
// new Foo(quux.foo().bar(baz('hello', 'there', foo[0].bar(42, arg))));
arg |> foo() |> bar
// SyntaxError: [|>] Expected function call
// 31: arg |> foo() |> bar
module Main where
import Prelude
import Control.Monad.Eff (Eff)
import Data.List (List(..), (:))
import Data.Maybe (Maybe(..))
import Halogen (ParentState, parentState, ParentQuery, Component, parentComponent, ParentHTML, Natural, ParentDSL, HalogenEffects, runUI)
import Halogen.HTML as H
import Halogen.Util (runHalogenAff, awaitBody)
import Unsafe.Coerce (unsafeCoerce)
module Main where
import Prelude
class Thunked a
instance thunkedAll :: Thunked a
type Thunk b = forall a. Thunked a => b