Skip to content

Instantly share code, notes, and snippets.

@turanct
turanct / Main.hs
Created July 3, 2017 13:45
Query Parser
import Text.ParserCombinators.Parsec
data Query = And Query Query
| Or Query Query
| Not Query
| Statement String deriving (Show)
andParser = do
arg1 <- statementParser
string " AND "
@turanct
turanct / Main.hs
Last active January 11, 2017 08:25
Haskell Coffee Run
import Shop
main = do
contents <- getContents
let
inventory1 = inventoryFromFile contents
shop1 = Shop "Simon Says" inventory1
putStrLn $ show shop1
@turanct
turanct / hangman.scm
Last active March 10, 2021 02:02
The Hangman game in Guile Scheme (lisp)
#!/usr/bin/env guile -s
!#
(use-modules
(srfi srfi-1)
(ice-9 rdelim))
(define words-path "words.txt")
(define random-word-from-file
@turanct
turanct / game-of-life.scm
Last active October 28, 2021 16:56
Super simple Conway's game of life in scheme
(define create-coord
(lambda (x y)
(list x y)))
(define get-x
(lambda (coord)
(car coord)))
(define get-y
(lambda (coord)
@turanct
turanct / mapreduce.scm
Created May 14, 2015 20:21
Monoids for map/reduce in scheme
(use-modules (srfi srfi-1) (ice-9 threads))
;; some funtions to generate page content
(define page-contents (list "foo" "bar" "baz" "qux"))
(define generate-page
(lambda (contents times)
(cond
((eq? 0 times) '())
(else (append contents (generate-page contents (- times 1)))))))
@turanct
turanct / run.php
Last active August 29, 2015 14:19
Monoids for map/reduce
<?php
require_once __DIR__ . '/util.php';
require_once __DIR__ . '/wordcount.php';
/**
* Create a document, containing 100 pages of 1000 times "foo bar baz qux"
*/
$page = str_repeat("foo bar baz qux\n", 1000);
$document = array_fill(0, 100, $page);
@turanct
turanct / higher-order.php
Last active August 29, 2015 14:18
Monoids for validation
<?php
function partial($function, ...$arguments)
{
return function() use ($function, $arguments) {
$fullArguments = array_merge($arguments, func_get_args());
return call_user_func_array($function, $fullArguments);
};
}
@turanct
turanct / list-functions-folds.scm
Last active August 29, 2015 14:15
Folds are awesome abstractions over recursive function calls
; As seen in my previous gist about lisp list-functions
; (https://gist.github.com/turanct/e84be7f355e255c2c1b5)
; all list functions can be created relatively easy from
; a few function calls, using recursive functions.
; As the pattern for those functions is almost always the same,
; that can be abstracted out. The way we do this is using folds.
;
; Folds take a function, an initial value (or carry), and a
; list, and they use the function to combine the carry with the
; next element of the list. When the fold iterated over the
@turanct
turanct / functional-tests.php
Created February 11, 2015 14:50
Simple test framework, functional programming style
<?php
function within($topic, ...$features)
{
return function($do = 'getFailedAssertions') use ($topic, $features) {
if ($do === 'getName') {
return $topic;
} elseif ($do === 'getFailedAssertions') {
return array_reduce(
$features,
@turanct
turanct / ycombinator.scm
Last active August 29, 2015 14:14
Simple fixed-point combinator in scheme
; Simple fixed point combinator. It's basic, but it does the job...
(define (fix function)
(lambda args
(apply function (cons function args))))
(define factorial
(fix
(lambda (f x)
(if (< x 2)