Skip to content

Instantly share code, notes, and snippets.

View islandjoe's full-sized avatar

Arthur Kho islandjoe

  • Helsinki, Finland
View GitHub Profile
@islandjoe
islandjoe / vapor-route-parameter.md
Created May 14, 2018 10:10
Replace a string part of the route with a Swift data type.
router.get("greets", String.parameter)
{ (req)-> String in 
  let name = try req.parameters.next(String.self)
  return "Hello, \(name)!"
}

This path will respond to requests for /greets/Broseph by printing out "Hello, Broseph!"

@islandjoe
islandjoe / vapor-route-paths-components.md
Created May 14, 2018 10:29
Listing path components in a Vapor route.
router.get("page", Int.parameter, String.parameter)
{ (req)-> String in
  let num = try req.parameters.next(Int.self)
  let section = try req.parameters.next(String.self)
  
  return "Loading page \(num), section \(section)."
}
@islandjoe
islandjoe / minimal-web-server.py
Last active May 28, 2018 10:36 — forked from bradmontgomery/dummy-web-server.py
A minimal http server in Python 3. Responds to GET, HEAD, POST requests, but will fail on anything else.
#!/usr/bin/env python3
"""
Very simple HTTP server in python.
Usage::
./dummy-web-server.py [<port>]
Send a GET request::
curl http://localhost
@islandjoe
islandjoe / python--argument-parser-config-file.md
Created May 29, 2018 17:59
How to implement Python 3 ArgumentParser with arg from a config file.
import argparse
import json

arg = argparse.ArgumentParser()
arg.add_argument("-c", "--conf", required=True, help="path to the JSON configuration file")

args = vars(arg.parse_args())

conf = json.load(open(args["conf"]))
@islandjoe
islandjoe / swift--map.md
Last active May 31, 2018 13:22
Swift mapping
let numbers = [1, 2, 3, 4, 5, 6, 7, 8]

Trailing closures

let squared = numbers.map() { (x)-> Int
  return x * x
}
@islandjoe
islandjoe / swift--filter.md
Created May 31, 2018 13:57
Filtering in Swift
let numbers = [1, 2, 3, 4, 5, 6, 7, 8]

Method 1

func isEven(x: Int) -> Bool {
  return x % 2 == 0
}
@islandjoe
islandjoe / swift--reduce.md
Created May 31, 2018 14:28
Reducing in Swift
let numbers = [1, 2, 3, 4, 5, 6, 7, 8]

Function as argument

func reducedSum(sum: Int, element: Int) -> Int {
  return sum + element
}
let sum = numbers.reduce(0, reducedSum)
@islandjoe
islandjoe / swift-partial-application.md
Created May 31, 2018 15:09
Partial application in Swift
func add(x: Int, y: Int, z: Int) -> Int {
  return x + y + z
}

let sum = add(x: 1, y: 2, z: 3)
print(sum)
//-> 6

func addX(_ x: Int) -&gt; (Int, Int)-&gt; Int {
@islandjoe
islandjoe / swift-recursion.md
Created May 31, 2018 15:21
Recursion in Swift
func countDown(from: Int) {
  // Stop condition
  if from < 0 {
    return
  }
  
  print(from)
  // Recursion
 countDown(from: from - 1)
@islandjoe
islandjoe / convert-to-coreml.md
Last active June 3, 2018 18:28
Convert CSV to CoreML model

First the modules. From your virtual environment:

$ mkvirtualenv coreml -p python3
$ workon coreml
(coreml)$ pip install -U pandas
(coreml)$ pip install -U sklearn
(coreml)$ pip install -U scipy
(coreml)$ pip install -U coremltools