Skip to content

Instantly share code, notes, and snippets.

@maruks
maruks / join.erl
Created February 28, 2017 00:32
functions over lists
-module(join).
-import(lists,[foldr/3,foldl/3,filter/2,split/2,flatmap/2,map/2,seq/2]).
-export([join/2,concat/1,member/2,qsort/1,msort/1,isort/1,perms/1]).
join_([X|Xs],Ys) ->
join_(Xs, [X | Ys]);
join_([], Ys) ->
Ys.
nub([]) ->
[];
nub([X|Xs]) ->
[X | nub (lists:filter(fun (E) -> E =/= X end, Xs)].
@maruks
maruks / bits.erl
Created February 24, 2017 21:02
futurelearn erlang assignment #1
-module(bits).
-export([bits/1, bits2/1, bits3/1]).
bits_tail_rec(<<B:1, Rest/bitstring>>, A) ->
bits_tail_rec(Rest,B + A);
bits_tail_rec(<<>>, A) ->
A.
bits(N) ->
-module(main).
-define(WINDOW_SIZE, 200000).
-define(MSG_COUNT, 1000000).
-define(MSG_SIZE, 1024).
-export([main/0]).
push_message(N, Tab) ->
Start = erlang:monotonic_time(),
Bin = list_to_binary(lists:duplicate(?MSG_SIZE, N rem 255)),
@maruks
maruks / main.erl
Created December 7, 2016 11:45
GC benchmark ported to Erlang
-module(main).
-define(WINDOW_SIZE, 200000).
-define(MSG_COUNT, 1000000).
-define(MSG_SIZE, 1024).
-export([main/0]).
push_message(N) ->
Start = erlang:monotonic_time(),
Bin = list_to_binary(lists:duplicate(?MSG_SIZE, N rem 255)),
isSorted :: Tree -> Bool
isSorted t = isSortedTree t minBound maxBound
insert :: Tree -> Int -> Tree
insert Leaf e = Node e Leaf Leaf
insert n@(Node v l r) e
| v < e = Node v l (insert r e)
| v > e = Node v (insert l e) r
| otherwise = n
@maruks
maruks / erl.el
Created September 22, 2016 22:09
use rebar3 erlang shell in emacs
(setq inferior-erlang-machine "rebar3")
(setq inferior-erlang-machine-options '("shell"))
(setq inferior-erlang-shell-type nil)
#lang racket
(define (mlist . elem)
(mcons (car elem)
(if (null? (cdr elem))
'()
(apply mlist (cdr elem)))))
;; has-cycle
@maruks
maruks / anaphoric.rkt
Last active May 23, 2016 17:35
anaphoric macro
(define-syntax (aif stx)
(syntax-case stx ()
[(_ expr then else)
(with-syntax ([it (datum->syntax stx 'it)] )
#'(let ((it expr))
(if it then else)))]))
@maruks
maruks / macro.rkt
Created May 6, 2016 00:25
cond-> in racket
(define-syntax thread-first
(syntax-rules ()
[(_ expr (fn args ...))
(fn expr args ...)]
[(_ expr fn)
(fn expr)]))
(define-syntax cond->
(syntax-rules ()
[(_ expr test fn)