Skip to content

Instantly share code, notes, and snippets.

@edwardGunawan
edwardGunawan / login-async.js
Created April 17, 2018 04:45
Login-async for main-process
const {ipcMain} = require('electron');
const db = require('../../db.js');
ipcMain.on('login-auth', (event,data) => {
console.log('data passed from login component', data);
// authentication is to generate hashvalue in bcrypt and check if the password exist
// however when the user close and get back in, they need to sign in again
// do authentication here
db.user.authenticate(data).then((user) => {
console.log('user here in login-async after authenticating',data);
@edwardGunawan
edwardGunawan / db.js
Created April 17, 2018 04:55
db setup for development
const Sequelize = require('sequelize');
const sequelize = new Sequelize(undefined, undefined, undefined, {
'dialect':'sqlite',
'storage': __dirname + '/data/dev-product.sqlite'
});
let db = {};
@edwardGunawan
edwardGunawan / TeamForm.jsx
Created October 22, 2019 13:57
Presentational Form
const TeamForm = () => {
return (
<>
<label htmlFor="admins">Team Name</label>
<input type="text" value={team.name} onChange={handleChange} name={'name'} placeholder="Team Name" />
<div>
<label htmlFor="admins">Admins</label>
<select name={"admins"} value={''} onChange={handleChange}>
{usernameList.map(({id, username}) =>
<option key={uuid.v4()} value={id}>{username}</option>
@edwardGunawan
edwardGunawan / EffectfulProgram.scala
Created June 22, 2020 03:37
What is "effect" or "effectful" mean in Functional Programming?
def division(num1:Int, num2:Int): Either[Exception, Int] = if(num2 == 0) {
Left(new IllegalStatement("num1 cannot divide by zero"))
} else {
Right(num1/num2)
}
@edwardGunawan
edwardGunawan / unfold.scala
Last active August 3, 2020 02:56
WTF is Corecursion
def unfold[A,S](s:S)(f: S => Option[(A,S)]): List[A] = f(s) match {
case Some((currentResult,nextState)) => currentResult :: unfold(nextState)(f)
case None => Nil
}
@edwardGunawan
edwardGunawan / traversal.scala
Created August 3, 2020 02:58
WTF is Corecursion
sealed trait Graph[+T]
case class Node[T](value:T, child:List[Graph[T]]) extends Graph[T]
def bfs[T](root:Graph[T]): List[T] =
unfold(List(root)){
case Node(value,child) :: t =>
val newQueue = t ++ child
Some((value, newQueue))
case Nil => None
@edwardGunawan
edwardGunawan / fib.scala
Created August 3, 2020 02:58
WTF is Corecursion
def fib(n:Int): Int = generateFibSequence(n).last
def generateFibSequence(n:Int): List[Int] =
unfold((n, (0,1))){s => s match {
case ((num, (f0, f1))) if(num > 0) => Some((f0, (num -1, (f1, f0+f1))))
case _ => None // terminate
}}
@edwardGunawan
edwardGunawan / factorial.scala
Created August 3, 2020 02:58
WF is Corecursion
def factorialGenerator(n:Int): List[Int] = unfold((1, 1)){
case ((num,_)) if (num > n) => None
case ((num, currFactorial)) =>
val nextFactorialValue = currFactorial * num
Some(( nextFactorialValue, (num+1, nextFactorialValue)))
}
@edwardGunawan
edwardGunawan / andWithThunk.scala
Created August 3, 2020 03:07
The Downside of Functional Data Structure
def and(a: () => Boolean, b: () => Boolean): Boolean = if(!a()) {
false
} else if(!b()) {
false
} else {
true
}
@edwardGunawan
edwardGunawan / andPassByName.scala
Created August 3, 2020 03:07
The Downside of Functional Data Structure
def and(a: => Boolean, b: => Boolean): Boolean =
if(!a) {
false
} else if(!b) {
false
} else {
true
}