Skip to content

Instantly share code, notes, and snippets.

View matemagyari's full-sized avatar

Mate Magyari matemagyari

View GitHub Profile
package main
import "fmt"
type (
employee interface {
yearContribution() float32
baseSalary() int
toString() string
}
(ns example
(:require [clojure.spec :as s]))
;; Spec section. It's not needed for the solution, but provides
;; documentation and can generate tests.
(def employee-types #{:office-worker :field-agent})
(s/def ::type employee-types)
(s/def ::years (s/int-in 0 100))
(s/def ::name string?)
sealed abstract class Employee(val name: String, years: Int, baseSalary: BigDecimal) {
//abstract method
def yearsContribution(): BigDecimal
//do the same logic with different data and sub-logic in each subclass
def salary(): BigDecimal = baseSalary + yearsContribution()
override def toString: String = s"My name is $name and I've been working here for $years years."
}
@matemagyari
matemagyari / principle.yml
Created May 7, 2016 12:14
Principle YAML
root_package: org.tindalos.principle
checks:
layering:
layers: [infrastructure, app, domain]
violation_threshold: 0
third_party_restrictions:
allowed_libraries:
@matemagyari
matemagyari / ReaderExample2.scala
Last active April 9, 2016 21:13
ReaderExample2
trait AccountService {
def credit(userId: Int, amount: Int): Reader[Context, Unit]
def debit(userId: Int, amount: Int): Reader[Context, Unit]
}
val service: AccountService = ???
val transfer: Reader[Context, Unit] = for {
_ ← service.credit(1, 10)
_ ← service.debit(2, 10)
@matemagyari
matemagyari / ReaderExample.scala
Last active April 9, 2016 21:11
ReaderExample
case class Reader[R, A](run: R ⇒ A) {
def map[B](f: A ⇒ B): Reader[R, B] = Reader(r ⇒ f(run(r)))
def flatMap[B](f: A ⇒ Reader[R, B]): Reader[R, B] = Reader(r ⇒ f(run(r)).run(r))
}
object UserService {
def notifyUser(user: User): Reader[Context, Unit] = Reader { context ⇒
context.userRepository.find(user.id) match {
case Some(foundUser) ⇒ context.notificationService.notifyUserAboutSth(foundUser)
case None ⇒ println("No user found!")
@matemagyari
matemagyari / HOFExample.scala
Last active April 9, 2016 21:12
HOFExample
case class Context(userRepository: UserRepository, notificationService: NotificationService)
object UserService {
def notifyUser(userId: Int): Context ⇒ Unit = context ⇒
context.userRepository.find(userId) match {
case Some(foundUser) ⇒ context.notificationService.notifyUserAboutSth(foundUser)
case None ⇒ println("No user found!")
}
}
@matemagyari
matemagyari / DIExample.scala
Last active April 9, 2016 20:27
DIExample
case class User(id: Int)
trait NotificationService {
def notifyUserAboutSth(user: User): Unit
}
trait UserRepository {
def find(id: Int): Option[User]
}
package org.bluecollar.scalaz
import scalaz.Scalaz._
import scalaz._
object StateMonadExamples extends App {
//Domain and test values
sealed trait Input
case object Coin extends Input
@matemagyari
matemagyari / gof_board.clj
Created December 10, 2015 08:37
Game of Life board
[" "
" ## "
" ## "
" ## "
" ## "
" "]