Skip to content

Instantly share code, notes, and snippets.

@edwardGunawan
edwardGunawan / chooseWithListOfStatement.scala
Created August 5, 2021 19:56
Functional Programming has made My Job Easier as a Software Engineer. Here's Why.
def choose(checkStatement: Int)(lstOfActions: List[(Int) => Unit]): Unit
@edwardGunawan
edwardGunawan / chooseWithTwoBoolean.scala
Created August 5, 2021 19:56
Functional Programming has made My Job Easier as a Software Engineer. Here's Why.
def choose(checkStatment: Boolean)(ifTrue: => Unit, ifFalse: => Unit): Unit
@edwardGunawan
edwardGunawan / reduceSemigroup.scala
Created August 5, 2021 19:54
Functional Programming has made My Job Easier as a Software Engineer. Here's Why.
def reduceSemigroup[A,B, F[_]](as:F[A])(f: A => B)(implicit F: Semigroup[B]): B
@edwardGunawan
edwardGunawan / fetchDBWithUnsafeRunSync.scala
Created August 5, 2021 19:53
Functional Programming has made My Job Easier as a Software Engineer. Here's Why.
val fetchDB: IO[Unit] = IO{
// some side effect (fetch DB print something)
fetchDB
}
val res = for {
_ <- fetchingDB
_ <- fetchingDB
} yield ()
@edwardGunawan
edwardGunawan / fetchDB.scala
Created August 5, 2021 19:53
Functional Programming has made My Job Easier as a Software Engineer. Here's Why.
val fetchingDB: Future[Unit] = Future{
// some side effect (fetch DB print something)
fetchDB
}
for {
_ <- fetchingDB
_ <- fetchingDB
} yield ()
@edwardGunawan
edwardGunawan / featchAndParseWithType.scala
Created August 5, 2021 19:52
Functional Programming has made My Job Easier as a Software Engineer. Here's Why.
def fetchAndParse(URL:String, client:Client): IO[Either[ParsingError,Json]]
@edwardGunawan
edwardGunawan / fetchAndParse.scala
Created August 5, 2021 19:50
Functional Programming has made My Job Easier as a Software Engineer. Here's Why.
def fetchAndParse(url:String, client:Client): Json = {
val res = client.fetch(url)
parse(res)
}
@edwardGunawan
edwardGunawan / handlePrepare.scala
Created March 18, 2021 12:27
How to Implement Paxos Algorithm in Pure Functions
def handlePrepare[V](msg: MessagePrepare[V]): State[Acceptor[V], Action] =
for {
acceptor <- State.get[Acceptor[V]]
(newAcceptor, action) = acceptor.promiseId match {
case Some(proposalId) if (msg.proposalId < proposalId) => (acceptor, Noop)
case _ =>
(
acceptor.copy(promiseId = Some(msg.proposalId)),
Send(
MessagePromise(proposalId = msg.proposalId, acceptor.maybeAccepted)
@edwardGunawan
edwardGunawan / Ops.scala
Created March 18, 2021 12:26
How to Implement Paxos Algorithm in Pure Functions
trait LearnerOps {
def handleAccepted[V](msg: MessageAccepted[V]): State[Learner[V], Unit]
def getValue[V]: State[Learner[V], Option[V]]
}
trait ProposerOps {
def sendPrepareProposal[V](message: MessagePrepare[V]): Action
def handlePromise[V](messages: List[MessagePromise[V]]): State[Proposer[V], Action]
def handleAccept[V](messages: List[MessageAccepted[V]]): State[Proposer[V], Action]
def constructHigherProposalId[V](prevProposal: Proposer[V]): Proposer[V]
@edwardGunawan
edwardGunawan / func.scala
Created March 18, 2021 12:23
How to Implement Paxos Algorithm in Pure Functions
def func(someArgument:SomeArgument): State[YourState, Action]