In most functional languages, you can write something like:
let rec fac = function
| 0 -> 1
| n -> n * fac (n - 1)
(defn times-two (x) | |
(* x 2)) | |
(defn div-by-three (x) | |
(= (% x 3) 0)) | |
;; With threading macro: | |
(->> | |
(range 100) | |
(filter div-by-three) | |
(map times-two) |
;; With infix arithmetic macro: | |
(defn f-real (cr ci zr zi) | |
~{ zr * zr - ir * ir + cr}) | |
(defn f-imag (cr ci zr zi) | |
~{ 2 * zr * zi + ci }) | |
(defn complex-abs (a b) | |
(sqrt ~{a * a + b * b})) | |
(defn m-loop (cr ci zr zi i) | |
(if ~{i = 0} | |
~{(complex-abs zr zi) < 2} |
(*****************************************************************************) | |
(******************************** UNIT TESTS *******************************) | |
(*****************************************************************************) | |
#use "hwk_01.ml" | |
(* even or odd *) | |
let () = | |
assert (even 4); | |
assert (not (even 5)) | |
(* Odd is not required *) |
use ::expr::Expr; | |
use nom::{hex_digit, oct_digit}; | |
use num::{BigInt, ToPrimitive}; | |
use std::mem; | |
named!(pub parse_number(&str) -> Expr, complete!(alt!( | |
based_number | |
))); | |
named!(foo(&str) -> &str, alt!( |
We're going to make a monad called Result
. First, define it as a type. This
definition makes it essentially an enum which is generic over two types.
It can either be Success
containing a value of the first type, or Error
containing a value of the second type.
data Result a e
use std::collections::btree_map::{BTreeMap, Entry}; | |
fn main() { | |
let mut m: BTreeMap<&'static str, i32> = BTreeMap::new(); | |
m.insert("foo", 4); | |
println!("=== Starting Foo ==="); | |
match m.entry("foo") { | |
Entry::Vacant(entry) => { | |
println!("{:?}", entry) |
<html version="-//W3C//DTD XHTML 2.0//EN" xml:lang="en" xmlns="http://www.w3.org/1999/xhtml"> | |
<head> | |
<META http-equiv="Content-Type" content="text/html; charset=UTF-8"> | |
<title>Copper HTML dump</title> | |
<style type="text/css"> | |
/* div.page { width: 800px; } */ | |
table.symtable, | |
table.dfatable { border: 1px solid #000000; border-collapse: collapse; } | |
span.lookaheadset { background-color: #d0d0d0; padding-left: 4pt; padding-right: 4pt; } | |
table.symtable tr.trodd { background-color: #d0d0d0; } |
export ZSH="${HOME}/.oh-my-zsh" | |
ZSH_THEME="ys" | |
plugins=(common-aliases compleat dircycle git git-extras pip python sudo tmux) | |
export EDITOR="vim" | |
export PATH="${HOME}/bin:${HOME}/.local/bin:${HOME}/.cabal/bin:${HOME}/.cargo/bin:${GOPATH}/bin:${PATH}" | |
export SILVERTRACE=1 | |
source $ZSH/oh-my-zsh.sh |