Skip to content

Instantly share code, notes, and snippets.

View marcoonroad's full-sized avatar
🦋
the Future() has been CANCELLED and we .Restart() the DEAD Event[] from the past

Marco Aurélio da Silva marcoonroad

🦋
the Future() has been CANCELLED and we .Restart() the DEAD Event[] from the past
View GitHub Profile
@marcoonroad
marcoonroad / fib.fth
Created October 5, 2014 23:21
Fibonacci written in Forth.
\ == recursive version ==
: fib
dup 2 <= if
drop 1
else
dup 1 - recurse
swap 2 - recurse +
then ;
@marcoonroad
marcoonroad / multi.pl
Created October 12, 2014 04:35
Um simples teste de funções com múltiplo despacho e recursão...
#!/home/user/localperl/bin/perl
use v5.20;
use strict;
use warnings;
use feature qw| say signatures |;
my %multi;
sub def ($name, $param, $lambda) {
@marcoonroad
marcoonroad / fac.pl6.txt
Last active August 29, 2015 14:07
Perl 6 way of Factorial function.
#!/usr/bin/perl6
use v6;
# overloading string 'x' operator
multi infix:<x> (Int \a, Int \b) { a * b }
# but we still want the string 'x' operator
multi infix:<x> (Str \a, Int \b where * > 1) { [~] gather for ^b { take a } }
@marcoonroad
marcoonroad / defac.lua
Last active August 29, 2015 14:07
Reverse factorial at Lua...
-- defac as multi-dispatch pseudo-code
--
-- defac(x): defac(x / 1, 1) [ where x is Integer ]
-- defac(x, d): d - x [ where x = 1 ]
-- defac(x, d): error() [ where x < 1 ]
-- defac(x, d): defac(x / d, d + 1) [ where x > 1 ]
function defac (x, d)
d = d or 1
if x == 1 then
@marcoonroad
marcoonroad / range.lua
Created October 14, 2014 18:16
A better range generator function at Lua...
-- as multi-dispatch pseudo-code
--
-- range(init, final, step):
---- return
---- [ where init > final ]
--
-- range(final):
---- return 0, range(0 + 1, final, 1)
---- [ where final is Positive Integer ]
--
@marcoonroad
marcoonroad / MultiFib.pl6
Created October 16, 2014 03:18
Fibonacci using multi-dispatch functions at Perl 6...
#!/usr/bin/perl6
use v6;
multi fib(Int $n where -> $n { $n ~~ any(1, 2) }) { 1 }
multi fib(Int $n where -> $n { $n > 2 }) { fib($n - 1) + fib($n - 2) }
say fib 10; # 55
@marcoonroad
marcoonroad / trabAL2.c
Last active August 29, 2015 14:07
Trabalho de AL2, simulando um sistema de mensagens.
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
// max & min size of strings
#define MIN 50
#define MAX 500
// limit of messages
#define MSGS 100
@marcoonroad
marcoonroad / multi-ack.pl6
Created October 17, 2014 19:49
Multi-Dispatch Ackermann function at Perl 6.
# ~~~ | n + 1 :@: if m = 0 ~~~ #
# ~~~ A(m, n) = | A(m - 1, 1) :@: if m > 0 and n = 0 ~~~ #
# ~~~ | A(m - 1, A(m, n - 1) :@: if m > 0 and n > 0 ~~~ #
multi A($m where(* == 0), $n ) { $n + 1 }
multi A($m where(* > 0), $n where(* == 0)) { A($m - 1, 1) }
multi A($m where(* > 0), $n where(* > 0)) { A($m - 1, A($m, $n - 1)) }
say A(1, 2);
@marcoonroad
marcoonroad / linkedmulti.pl6
Created October 22, 2014 17:32
Linked/chained list generator multi-dispatch function written at Perl6.
#!/usr/bin/perl6
use v6;
multi chain ([ ]) { [ head => [ ] ] }
multi chain (@Ls) { [ head => @Ls.shift, tail => chain @Ls ] }
# test
my @chained = chain [ 1, 2, 3 ];
say @chained.perl;
@marcoonroad
marcoonroad / ifelse_methods.lua
Last active August 29, 2015 14:08
If and Else as methods (as like Smalltalk)...
-- if and else as methods (test) --
-- object --
local logic = { main = nil }
-- if method --
function logic: so(code)
if self.main then
code( )
end