This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
; A Racket solution similar to the one-line Ruby solution. | |
(require racket/base) ; Needed for in-range | |
(require racket/sequence) ; Needed for sequence->list | |
(foldl + 0 | |
(filter (lambda (n) (or (= (remainder n 3) 0) | |
(= (remainder n 5) 0))) | |
(sequence->list (in-range 0 1000)))) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(ns four-clojure-problems.graph-connectivity) | |
(use 'clojure.test) | |
;; Take a connection map (maps nodes to sets of | |
;; nodes that are all the nodes the key node | |
;; connects to) and an edge, and returns a new, | |
;; updated connection map. | |
(defn process-connection [connections-map edge] | |
(let [a (first edge) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(ns examples.age-person) | |
;; Based on an example from Chap 4 of Programming Clojure | |
;; by Emerick, et al | |
(defmacro futures | |
[n & exprs] | |
(vec (for [_ (range n) | |
expr exprs] | |
`(future ~expr)))) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(ns examples.characters) | |
;; Based on an example from Chap 4 of Programming Clojure | |
;; by Emerick, et al | |
(defmacro futures | |
[n & exprs] | |
(vec (for [_ (range n) | |
expr exprs] | |
`(future ~expr)))) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
;;;;;;;;;;;;;;;;;; | |
;; The first way | |
;;;;;;;;;;;;;;;;;; | |
;; This use of atom is simple and works fine. | |
(def counter (atom -1)) | |
(defn next-value [] | |
(swap! counter inc)) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
;;;;;;;;;;;;;;;;;;;;;;;;;;; | |
;; This logging code is taken straight from Chapter 4 of | |
;; Programming Clojure by Emerick, et al. If you're having | |
;; trouble finding your console output in Eclipse using | |
;; counterclockwise, add the file output from the book and | |
;; use that instead. | |
(def console (agent *out*)) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
%% @author mcphee | |
%% @doc A simple example showing that messages that | |
%% aren't immediately processed aren't lost, but are | |
%% held for possible matching later. | |
-module(message_queue). | |
-export([wait/0, run/0]). | |
% P = spawn(message_queue, wait, []) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
%% @author mcphee | |
%% @doc An example in Erlang of a crytographically fair | |
%% distributed coin toss. | |
% There are a ton of commented out erlang:display/1 calls that illustrate | |
% a fairly brute force approach to debugging this sort of code. It can be | |
% really difficult to tell what's going on in a process, and using erlang:display/1 | |
% (which should *only* be used for debugging instead of "real" I/O) can be | |
% a useful way to see what's happening. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
%% @author mcphee | |
%% @doc An example in Erlang of a crytographically fair | |
%% distributed coin toss. | |
-module(dist_coin_toss). | |
%% ==================================================================== | |
%% API functions | |
%% ==================================================================== | |
-export([run/3, pair/2, pair_all/1, create_people/3, make_person/3, counter_loop/2, wait/1, wait_for_choice/5, wait_for_outcome/4]). |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import wordWrap | |
import unittest | |
import sys | |
class TestWordWrap(unittest.TestCase): | |
def setUp(self): | |
self.string = "Andrew is a pretty cool dude!" | |
def tearDown(self): |
OlderNewer