Skip to content

Instantly share code, notes, and snippets.

Avatar
🎯
Focusing

Xiaoyun Yang xiaoyunyang

🎯
Focusing
View GitHub Profile
@xiaoyunyang
xiaoyunyang / curry_1.scala
Last active Jan 20, 2018
Functional Programming in Scala
View curry_1.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 Jan 20, 2018
Error Handling in Node/Express App
View express-handle-error.js
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 Aug 6, 2019
Closure Example - Object Oriented Programming
View closure_oo_example.js
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 Jan 25, 2018
Closure Example - Recursion
View closure_with_recursion_naive.js
var incrementUntil = function(max) { 
if(num >= max) return num 
num++
incrementUntil(max)
}
 
var num = 0
incrementUntil(3)
num //> 3
View closure_with_recursion_myFun.js
num = 0
var myFun = function() { 
incrementUntil(3)
  return num 
}
myFun() //> 3
num //> 3
View closure_with_recursion_myFun2.js
num = 0
var myFun2 = function() { 
var num = -1
incrementUntil(3)
return num 
}
myFun2() //> -1 ……Why?
num //> 3
@xiaoyunyang
xiaoyunyang / sample_node.js
Last active Jan 25, 2018
Closure Example Actual
View sample_node.js
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 Jan 25, 2018
JavaScript Currying
View curry.js
var multThenAdd = num => {
return mul => add => {
return num * mul + add
}
}
var timesTwoPlusFour = (num) => multThenAdd(num)(2)(4)
timesTwoPlusFour(1) //> 6
timesTwoPlusFour(10) //> 24
View closure_with_recursion_good_fix.js
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
View closure_with_recursion_bad_fix.js
var myFun2 = function() {
var num = -1
function incrementUntil(max) {
if(num >= max) return num
num++
incrementUntil(max)
}
incrementUntil(3)
return num
}