Skip to content

Instantly share code, notes, and snippets.

View johnwesonga's full-sized avatar

johnwesonga

  • Bay Area
View GitHub Profile
@johnwesonga
johnwesonga / factorial-clojure.clj
Created May 2, 2020 04:57
Factorial in Clojure
(defn fact
[x]
(cond
(zero? x) 1
(= x 1) 1
:else (* x (fact (dec x)))))
@johnwesonga
johnwesonga / .clj
Last active April 28, 2020 05:57
Remove map from vector of maps
(def mm '[{:a 1, :b 2} {:a 2, :b 3}])
(map #(dissoc % :a) mm)
@johnwesonga
johnwesonga / vector to map
Last active May 2, 2020 04:56
Clojure: Convert vector to map
(def header-lines ["Host: example.com" "User-Agent: ExampleBrowser/1.0" "Accept: */*"
"Content-Type: application/x-www-form-urlencoded" "Content-Length: 21"])
(:require '[clojure.string :as str])
(->> (map #(let [[k v] (str/split % #": ")]
[(keyword k) v]) header-lines)
(into {}))
;;str/split function
@johnwesonga
johnwesonga / Drivers.re
Created August 9, 2019 17:27
reasonml+graphql
module GetDrivers = [%graphql
{|
query getDrivers{
all_drivers {
id
name
email
}
}
|}
@johnwesonga
johnwesonga / types.elm
Last active July 24, 2019 06:02
elm type alias
type SortDirection = Asc | Desc
type alias SortedLabels = {labels: List String , direction: SortDirection }
type alias Article = { name: String, keywords: SortedLabels }
addArticles: String -> List Article -> List Article
addArticles name article =
Article name (SortedLabels [] Asc) :: article
--Call the function
addArticles "food" [Article "start" (SortedLabels ["foo", "bar"] Asc), Article "start" (SortedLabels ["foo", "bar"] Asc)]
@johnwesonga
johnwesonga / factorial.pur
Last active March 18, 2018 22:19
Factorial in PureScript
module Main where
import Prelude (show, ($), (=<<), (+), (<>), (*), (-))
import Control.Monad.Eff.Console(log)
import TryPureScript
fact :: Int -> Int
fact 0 = 1
fact n = n * fact(n-1)
@johnwesonga
johnwesonga / factorial.elm
Last active May 6, 2022 18:03
Factorial in Elm
import Html exposing (text)
fact : Int -> Int
fact n =
case n of
0 -> 1
1 -> 1
_ -> n * fact(n-1)
factorial : Int -> Int
module Main exposing (..)
import Html exposing (Html, div, fieldset, input, label, text)
import Html.Attributes exposing (name, style, type_)
import Html.Events exposing (onClick)
-- MODEL
@johnwesonga
johnwesonga / readints.go
Created December 14, 2016 18:38
Read ints with whitespaces from stdin
package main
import "fmt"
func main() {
fmt.Println(`Enter the number of integers`)
var n int
if m, err := fmt.Scan(&n); m != 1 {
panic(err)
}
@johnwesonga
johnwesonga / emberjs-routing
Last active August 29, 2015 14:19
Emberjs routing headache
Steps:
Create new ember project- "ember new todo"
Add adapter to project - "ember g adapter application"
Add model to project - "ember g model todo task:string completed:boolean"
Add route to project - "ember g route todo"
Sub Steps:
Create fixtures for model:
import DS from 'ember-data';
let Todo = DS.Model.extend({