Skip to content

Instantly share code, notes, and snippets.

@pzel
pzel / maybe.pony
Created February 26, 2019 17:25
Maybe primitive
type MaybeT[A] is (A|None)
primitive Maybe
fun just[A: Any val](a: A) : MaybeT[A] => a
fun nothing[A: Any val]() : MaybeT[A] => None
fun fmap[A: Any val, B: Any val](v: MaybeT[A], f: {(A): B}): MaybeT[B] =>
match v
| None => None
| let v' : A => f(v')
end
@pzel
pzel / functor.pony
Created February 26, 2019 17:12
Maybe & Result implemenations of Functor Interface
class val Maybe[A: Any val] is Functor[A]
let _v : (A | None)
new val just(a: A) => _v = a
new val nothing() => _v = None
fun val fmap[B: Any val](f: {(A): B}) : Maybe[B] =>
match _v
| None => Maybe[B].nothing()
| let v' : A => Maybe[B].just(f(v'))
end
@pzel
pzel / functor.pony
Last active February 26, 2019 10:05
A maybe type, with Functor interface, in Pony
class val Maybe[A: Any val]
let _v : (A | None)
new val just(a: A) => _v = a
new val nothing() => _v = None
fun val fmap[B: Any val](f: {(A): B}) : Maybe[B] =>
match _v
| None => Maybe[B].nothing()
| let v' : A => Maybe[B].just(f(v'))
end
@pzel
pzel / Makefile
Created February 21, 2019 22:15
Makefile
.setup: $(wildcard deps/**)
@echo setting up
@touch .setup
test: .setup
@echo tests ran
class Maybe[A : Equatable[A] val] is Equatable[Maybe[A]]
let v : (A val | None)
new val create(thing: A) => v = thing
new val empty() => v = None
fun eq(that: box->Maybe[A]): Bool =>
match (this.v, that.v)
| (let v' : A, let v'' : A) => v' == v''
| (let n' : None, let n'' : None) => true
@pzel
pzel / example.pony
Last active May 25, 2018 23:18
Returning internal state as a val to a promise
// This works
actor Router
let _env : Env
var _notifiers : Map[String val, ChatSession tag]
new create(env: Env) =>
_env = env
_notifiers = Map[String val, ChatSession tag].create(10)
fun ref _user_list() : Array[String] val =>
@pzel
pzel / UTF8-trim.md
Last active May 21, 2018 21:29
String.strip() inconsistency

Unintiuitive behavior in String.strip()

I just ran across unintuitive behavior in [String.strip]

When the last U8 preceding the trailing whitespace is a fragment of a utf-8 sequence, it gets chopped off along with the following whitespace.
Whe the last U8 preceding the end of the string is a fragment of a utf-8 sequence, that last codepoint gets mangled to its first octet.

This goes against the prinicple of least surprise. Encoding issues notwithstanding,

@pzel
pzel / ct_tty_hook.erl
Created July 10, 2014 13:52
Reasonable TTY output for common tests.
%%% @doc Common Test Example Common Test Hook module.
-module(ct_tty_hook).
%% @doc Add the following line in your *.spec file to enable
%% reasonable, tty error reporting for your common tests:
%% {ct_hooks, [ct_tty_hook]}.
%% Callbacks
-export([id/1]).
-export([init/2]).

Project Build Tools: Output on first run vs. second run with no new changes

Cabal-install (Haskell), initial run:

Running cabal install for the first time, we get a full report of the steps taken to build the project.

hproject$ cabal install
Resolving dependencies...
p@maple:/usr/local/src/camping$ git diff
diff --git a/Gemfile b/Gemfile
index b0b1ee2..aee38c4 100644
--- a/Gemfile
+++ b/Gemfile
@@ -9,6 +9,12 @@ if rack = ENV['RACK']
end
end
+group :development do