Skip to content

Instantly share code, notes, and snippets.

-- Fixed point registers
data Rx = R0 | R1 | R2 | R3 | R4 | R5 | R6 | R7 deriving (Show, Eq, Ord)
-- Floating point registers
data Fx = F0 | F1 | F2 | F3 | F4 | F5 | F6 | F7 deriving (Show, Eq, Ord)
data Expr :: * -> * where
AddI :: Rx -> Rx -> Expr Integer
SubI :: Rx -> Rx -> Expr Integer
AddF :: Fx -> Fx -> Expr Float
SubF :: Fx -> Fx -> Expr Float
{-# OPTIONS_GHC -fno-warn-orphans #-}
{-# LANGUAGE DataKinds #-}
{-# LANGUAGE TypeOperators #-}
{-# LANGUAGE QuasiQuotes #-}
{-# LANGUAGE FlexibleInstances #-}
{-# LANGUAGE OverloadedStrings #-}
import Control.Monad (void)
import Ivory.Language
import Ivory.Compile.C.CmdlineFrontend
import Ivory.Compile.C.Modules
@fredyr
fredyr / comments.js
Created February 27, 2014 19:32
React javascript version
var comments = [
{author: "Pete Hunt", text: "This is a text"},
{author: "Suvash", text: "This is *another* comment"}
];
// Data is local to components,
// passed down from the parent
// this.props - immutable
-- Arne Andersson - Balanced Search Trees Made Simple
-- Original article: http://user.it.uu.se/~arnea/ps/simp.pdf
-- Wikipedia entry: http://en.wikipedia.org/wiki/AA_tree
data AATree a = E | T a (AATree a) (AATree a) Int deriving (Show, Eq)
insert E x = T x E E 1
insert (T y left right level) x
| x < y = balance $ T y (insert left x) right level
| otherwise = balance $ T y left (insert right x) level
@fredyr
fredyr / om-table.clj
Created January 21, 2014 09:43
Removing a table row failed in om/react when tbody was missing.
(ns om-table.core
(:require [om.core :as om :include-macros true]
[om.dom :as dom :include-macros true]))
(enable-console-print!)
(extend-type string
ICloneable
(-clone [n] (js/String. n)))
(def app-state (atom {:table ["very" "undefined" "such" "parentNode"]}))
;; Updated tutorial code for Om 0.1.6, 2014-01-16
;; http://www.lexicallyscoped.com/2013/12/25/slice-of-reactjs-and-cljs.html
;; See comments below for details on the changes.
(def app-state
(atom {:comments [{:author "Pete Hunt" :text "This is a comment."}
{:author "Jordan Walke" :text "This is *another* coment"}]}))
(defn comment [{:keys [author text]} owner]
(om/component
f x = (42-x)^2
impr = [(x, f x) | x <- [0, 0.1 ..]]
opt goal = head $ dropWhile (\(x, err) -> err >= goal) impr
(defn book [state owner]
(prn state)
(om/component
(dom/div nil (dom/h2 nil (:author state)))))
;;=>
{:author "Richard K. Morgan", :book "Altered Carbon"} core.cljs:55
Uncaught TypeError: Cannot read property 'string' of undefined core.cljs:110
;; Manual creation of a cursor giving the same behaviour
;; Inserted this snippet inside an om/component
(loop [foo (seq [])
bar [1 2 3]]
(if (empty? bar)
foo
(recur (conj foo (first bar)) (next bar))))
rows = 3
glasses = [
0.0,
0.0, 0.0,
0.0, 0.0, 0.0,
0.0, 0.0, 0.0, 0.0 ]
def index(i, j):
"ith glass in the jth row"
return i + sum(range(1, j+1))
def dist(v, i, j):