This file contains 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
;; gorilla-repl.fileformat = 1 | |
;; ** | |
;;; # Plotting some evolved sine results | |
;;; | |
;;; I decided to play with [Gorilla Repl](http://gorilla-repl.org) to try to plot some of the evolved Sine results just to make sure we were solving the problem we thought we were solving. | |
;;; | |
;;; The short version is that Gorilla Repl was quite excellent, and we are indeed solving the sine problem :-) | |
;;; | |
;;; One of the nifty things is that Clojure allowed me to define the symbol % to be protected division, so we can paste in functions straight from ECJ and run them without having to do any annoying search/replace action to get things in a format that our graphing tool can process. |
This file contains 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
public class ConvertingSignedBytes { | |
public static void main(String[] args) { | |
byte x = (byte) 37; | |
byte y = (byte) 150; | |
printByte(x); | |
printByte(y); | |
} | |
public static void printByte(byte b) { | |
int value; |
This file contains 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 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
# Person A flips and Person B chooses "heads" or "tails". | |
# Person B wins if their choice matches the flip, otherwise | |
# person A wins. | |
defmodule CoinToss do | |
def run_simulation(num_people) do | |
counterPid = spawn(__MODULE__, :counter_loop, [0, 0]) | |
Process.register(counterPid, :counter) | |
people = create_people(num_people) |
This file contains 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
# Person A flips and Person B chooses "heads" or "tails". | |
# Person B wins if their choice matches the flip, otherwise | |
# person A wins. | |
defmodule CoinToss do | |
def run_simulation(first_host, second_host, num_people) do | |
counterPid = spawn(__MODULE__, :counter_loop, [0, 0]) | |
:global.register_name(:counter, counterPid) | |
people = create_people(first_host, second_host, num_people) |
This file contains 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 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 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 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 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]). |
OlderNewer