Skip to content

Instantly share code, notes, and snippets.

View jasonneylon's full-sized avatar

Jason Neylon jasonneylon

View GitHub Profile
@jasonneylon
jasonneylon / blackjack.re
Created February 4, 2021 14:54
Blackjack functional domain modelling example team 2
type rank = Ace | Two | Three| Four | Five | Six | Seven| Eight | Nine | Ten | Jack | Queen | King ;
type suit = Spade | Hearts | Diamond | Club
type card = { rank, suit }
type deck = list(card)
/* type deck = List(card) | EmptyDeck */
/* draw card(s) (don't forget the effect on the deck)
ensure the deck is shuffled
@jasonneylon
jasonneylon / recent.sh
Created August 23, 2017 10:40
Everything I have done since a specfic date in all the git subfolders
for i in *; do ( cd $i; pwd; git --no-pager log --since="2016-02-12T16:36:00-07:00" --author jason --format=oneline; pwd ); done | less
(defn timeout [ms]
(let [c (chan)]
(js/setTimeout (fn [] (close! c)) ms)
c))
(defn run-kmean
[]
(go
(dotimes [iteration 10]
(<! (timeout 1000))
subscriptions : Model -> Sub Msg
subscriptions model =
Time.every second NextRunTicktions : Model -> Sub Msg
randomPoints : Generator (List(Point))
randomPoints =
Random.list 200 (Random.map (\(x, y) -> Point x y) (Random.pair (int 1 999) (int 1 399)))
-- here we create a command that the elm runtime will execute and then pass the results back via the Update function
initialModel : (Model, Cmd Msg)
initialModel =
(Model [] [] 6 0 False, (Random.generate DrawPoints randomPoints))
(defn random-point []
{:x (rand-int 1000) :y (rand-int 500)})
@jasonneylon
jasonneylon / passphrases.clj
Created November 30, 2016 10:10
Generate passphrases (and DNA!) using clojure.spec
(require '[clojure.spec :as s])
(require '[clojure.spec.gen :as gen])
;; PASSPHRASES - generate a random 5 word passphrase
;; naive approach was to generate a random string and contrain to match a five word pattern
;; however this hit 100 generation limit imposed by clojure.spec
(defn words [str] (clojure.string/split str #" "))
(def five-words? (comp (partial = 5) count words))
(gen/generate (s/gen (s/and string? five-words?)))
// publish an event when a user selects and address in our React component
onAddressSelected(addressIndex) {
this.state.selectedAddressIndex = addressIndex;
this.state.address = Object.assign({}, this.state.addresses[index]);
this.setState(this.state);
// using tinypubsub to wrap our js events
$.publish('address.selected', {address: this.state.address, type: this.props.addressType});
}
....
@jasonneylon
jasonneylon / submit-form-validating-react-component.js
Created October 22, 2015 16:44
Example of submitting form and validating javascript
// When submitting the form we use standard jquery validation
$form.submit(function handleSubmit(ev) {
// call the React address component instance and ask it to validate itself and return any errors
const errors = addressInstance.validate();
// if there are errors cancel the form submit
if(errors.length > 0) {
ev.preventDefault();
}
....
}