Skip to content

Instantly share code, notes, and snippets.

@nodename
nodename / lazy-seqs.js
Last active November 23, 2020 07:22
Functional programming with lazy sequences in JavaScript
// A lazy sequence is an object that has several methods: an API.
// A star function RETURNS a lazy sequence:
const naturals = function* () {
let i = 1;
while (true) { // infinite loop generates an infinite sequence!
yield i++;
}
};
@nodename
nodename / core.cljs
Created December 1, 2015 08:35
messed up om/next with react native
(ns hello.core
(:require [om.next :as om :refer-macros [defui]]))
(enable-console-print!)
(def React (js/require "react-native"))
(def AppRegistry (aget React "AppRegistry"))
(def StyleSheet (aget React "StyleSheet"))
(def create-element (aget React "createElement"))
@nodename
nodename / SlideShow.elm
Last active October 13, 2015 03:47
This Elm program compiles to a Web page that plays a movie of image files, reversing direction when it reaches the last or first image
import Time (every)
import Window (dimensions)
firstImageIndex = 0
lastImageIndex = 1000
delta = lift inSeconds (fps 30)
modCount signal period = lift (\a -> a `mod` period) (count signal)
data PlayDirection = Fwd | Back
@nodename
nodename / morph-curves.rkt
Created October 27, 2012 04:48
Racket program to create image sequence morphing from eight-petal rose to Lissajous curve
#lang racket
(require plot)
(require plot/utils)
(define (plot-zeros-to-file xy-function file-name epsilon)
(parameterize ([plot-x-axis? #f]
[plot-x-label #f]
[plot-x-far-axis? #f]
[plot-y-axis? #f]
[plot-y-label #f]
@nodename
nodename / breaking_dollars.clj
Created February 27, 2012 05:32
HW6-breaking-dollars failed solution
(ns breaking-dollars)
(defn first-coin-choices [cents denominations already-chosen]
(let [possible-choices (filter #(<= % cents) denominations)]
(if (empty? already-chosen)
possible-choices
; choose descending order to eliminate permutations:
(filter #(<= % (last already-chosen)) possible-choices))))
@nodename
nodename / map-while-spec.clj
Created February 26, 2012 19:49
H%5: map-while
(ns ex5.map-while-spec
(:use ex5.map-while)
(:use clojure.test))
(deftest test-map-while-1
(let [f #(* % %)
s (range 100)
pred #(< % 50)
result (map-while-1 f s pred)]
(is (= (count result) 50))
@nodename
nodename / account.clj
Created February 20, 2012 01:06
Coding Exercise #2: Fantasy Banking
(ns account
(:import [java.util Date]
[java.lang Math])
(:use [util]))
(def account-type [:checking :savings :money-market])
(def interest-rate (zipmap account-type [0.02 0.04 0.06]))
(def overdraft-charge (zipmap account-type [0.10 0.075 0.05]))
@nodename
nodename / rovers.clj
Created February 6, 2012 07:00
Coding exercise #1: Mars Rovers
(ns rovers
(:use [clojure.string :only [split-lines]]))
(def *test-input*
"5 5
1 2 N
LMLMLMLMM
3 3 E
MMRMMRMRRM")
@nodename
nodename / test.clj
Created December 20, 2011 07:25
My first Clojure test! Create an image, save it as a PNG file with a custom metadata string, and read back the string from the file header
(import '(java.awt.image BufferedImage))
(import '(javax.imageio ImageIO IIOImage))
(import '(javax.imageio.stream FileImageOutputStream))
(import '(com.sun.imageio.plugins.png PNGMetadata PNGImageReader))
(import '(java.io ByteArrayOutputStream File))
;; See the following document for requirements
;; for upper- and lower-case letters in the four-letter chunk name:
;; http://en.wikipedia.org/wiki/Portable_Network_Graphics#.22Chunks.22_within_the_file
(def generator-chunk-name "gnTr")
@nodename
nodename / ModelSnippet.as
Created November 29, 2011 04:22
AS3 setter question: model.selectedMetric = x; where x already is the selectedMetric. The setter is NOT invoked. Why?
private var _selectedMetric:Metric;
[Bindable]
public function get selectedMetric():Metric
{
return _selectedMetric;
}
public function set selectedMetric(value:Metric):void
{