Skip to content

Instantly share code, notes, and snippets.

View HackerFoo's full-sized avatar

Dusty DeWeese HackerFoo

View GitHub Profile
/*
__ calc_func: [=s] dip21 [.] pushl dip11!
calc_step:
strtrim
[<-str int_t pushr]
["+" =s [[+] .] dip11 !] |
["-" =s [[-] .] dip11 !] |
["*" =s [[*] .] dip11 !] |
["/" =s [[/] .] dip11 !] |
["gcd" =s [[gcd] .] dip11 !] |
/*
calc_step:
strtrim
[<-str int_t pushr]
["+" =s [[+] .] dip11 !] |
["-" =s [[-] .] dip11 !] |
["*" =s [[*] .] dip11 !] |
["/" =s [[/] .] dip11 !] |
["gcd" =s [[gcd] .] dip11 !] |
/*
calc_step:
strtrim
[<-str int_t pushr]
["+" =s [[+] .] dip11 !] |
["-" =s [[-] .] dip11 !] |
["*" =s [[*] .] dip11 !] |
["/" =s [[/] .] dip11 !] |
["swap" =s [[swap] .] dip11 !] |
["dup" =s [[dup] .] dip11 !] |
/* Popr source:
__ decreasing list from initial element
iota_: [[dup 1-] .] [head 0 >] iterate
iota: [1-] pushl iota_
*/
#include <stdio.h>
#include <stdlib.h>
@HackerFoo
HackerFoo / algorithm_sum.c
Last active June 7, 2017 14:12
algorithm.sum
/*
Popr source:
__ sums numbers in a list
sum_: [[+] .] [popr drop is_nil not] [head 0+] binrec
sum: 0 pushr sum_
*/
// compiled to C
int algorithm_sum(array func0)
@HackerFoo
HackerFoo / staging_exp_.c
Last active April 28, 2017 04:28
PoprC code generation for exponentiation by squaring algorithm
/* Popr code:
odd: 1 .& 1 ==
__ acc x power -> if odd power, acc*x, otherwise acc
acc_odd_power: [[*] dip21 odd] ap20 ifdo
__ x power -> x*x power/2
reduce_power: [dup *] [1 .>>] para
$ clang -O0 curry.c -o curry
$ ./curry a 1 2 3
pc = 0lx0000000000400cd9
f1_f = 10
c[48] =
[ 0x55, 0x48, 0x89, 0xe5, 0x48, 0x83, 0xec, 0x10,
0x48, 0xb8, 0x00, 0x07, 0x40, 0x00, 0x00, 0x00,
0x00, 0x00, 0x89, 0x7d, 0xfc, 0x48, 0x89, 0x45,
0xf0, 0x48, 0x8b, 0x04, 0x25, 0x50, 0x20, 0x60,
0x00, 0x48, 0x8b, 0x7d, 0xf0, 0x8b, 0x75, 0xfc,
@HackerFoo
HackerFoo / SelectBy.hs
Created May 29, 2013 01:43
Selection algorithm in Haskell
module SelectBy (selectBy, medianBy, select, median)
where
import Data.List
splitEvery _ [] = []
splitEvery n xs = xn : splitEvery n r
where (xn, r) = splitAt n xs
-- | O(n) kth smallest selection