Skip to content

Instantly share code, notes, and snippets.

View xiaoyunyang's full-sized avatar
🎯
Focusing

Xiaoyun Yang xiaoyunyang

🎯
Focusing
View GitHub Profile
@xiaoyunyang
xiaoyunyang / curry_1.scala
Last active January 20, 2018 04:30
Functional Programming in Scala
//Currying is when you break down a function that takes multiple arguments into a //series of functions that take part of the arguments.
//converts a function f of two arguments into a function of one argument that partially applies f.
def curry[A,B,C](f: (A,B) => C): A => (B => C) =
(a: A) => (b: B) => f(a,b)
//> curry: [A, B, C](f: (A, B) => C)A => (B => C)
@xiaoyunyang
xiaoyunyang / express-handle-error.js
Last active January 20, 2018 05:33
Error Handling in Node/Express App
import express from 'express'
import path from 'path'
import fs from 'fs'
const app = express()
app.set("port", process.env.PORT || 3001)
// Middleware that serves static file ======================================
app.use('/static', (req, res, next) => {
@xiaoyunyang
xiaoyunyang / closure_oo_example.js
Last active August 6, 2019 07:43
Closure Example - Object Oriented Programming
function Person(name) {
var secret = “secret!”
this.name = name
this.setName = function(newName) { this.name = newName }
this.setNameToFoo = function() { this.name = foo }
this.getSecret = function() { return secret }
}
 
var a = new Person(“Max”)
 
@xiaoyunyang
xiaoyunyang / closure_with_recursion_naive.js
Last active October 2, 2021 15:40
Closure Example - Recursion
var incrementUntil = function(max) { 
if(num >= max) return num 
num++
incrementUntil(max)
}
 
var num = 0
incrementUntil(3)
num //> 3
@xiaoyunyang
xiaoyunyang / closure_with_recursion_myFun.js
Last active January 25, 2018 09:06
Closure With Recursion
num = 0
var myFun = function() { 
incrementUntil(3)
  return num 
}
myFun() //> 3
num //> 3
@xiaoyunyang
xiaoyunyang / closure_with_recursion_myFun2.js
Last active January 25, 2018 09:06
Closure With Recursion
num = 0
var myFun2 = function() { 
var num = -1
incrementUntil(3)
return num 
}
myFun2() //> -1 ……Why?
num //> 3
@xiaoyunyang
xiaoyunyang / sample_node.js
Last active January 25, 2018 09:05
Closure Example Actual
const getInfoFromURL = path => {
const URL = require(“url”).URL;
  const myUrl = new URL(path)
  const pathname = myUrl.pathname
const getUsernameFromURL = pathname => {
  const regex = new RegExp(‘/@’);
  const username = pathname.split(regex).slice(1)[0]
  if(!username) {
return “Error in parsing: URL needs to be in format://hostname:port/@username”
@xiaoyunyang
xiaoyunyang / curry.js
Last active January 25, 2018 08:56
JavaScript Currying
var multThenAdd = num => {
return mul => add => {
return num * mul + add
}
}
var timesTwoPlusFour = (num) => multThenAdd(num)(2)(4)
timesTwoPlusFour(1) //> 6
timesTwoPlusFour(10) //> 24
@xiaoyunyang
xiaoyunyang / closure_with_recursion_good_fix.js
Last active January 25, 2018 09:01
Closure With Recursion
var incrementUntil = function(max) {
var inc = function(num) {
if(num >= max) return num
return inc(num+1)
}
return (num) => inc(num)
}
incrementUntil(4)(1) //> 4
incrementUntil(8)(3) //> 8
@xiaoyunyang
xiaoyunyang / closure_with_recursion_bad_fix.js
Last active January 25, 2018 09:00
Closure With Recursion
var myFun2 = function() {
var num = -1
function incrementUntil(max) {
if(num >= max) return num
num++
incrementUntil(max)
}
incrementUntil(3)
return num
}