Skip to content

Instantly share code, notes, and snippets.

@JYCabello
JYCabello / gradleProject.ps1
Last active July 5, 2022 14:11
PowerShell script to create a gradle project and open it with idea
function Idea {
param (
$PROJECT
)
$IDEA_PATH="YOURPATHTOIDEA\bin\idea64.exe"
Invoke-Expression "& '$IDEA_PATH' $PROJECT"
}
function CloneIdea {
param (
@JYCabello
JYCabello / FruitRepositoryIO.kt
Last active December 2, 2019 07:32
Using IO for the fruit repository insertOrUpdate
import arrow.core.None
import arrow.core.Option
import arrow.core.some
import arrow.fx.IO
fun insertOrUpdate(fruit: FruitMetadata): IO<Int> =
findByAuthorityId(fruit.fruitAuthorityId)
.flatMapNone { findByCouncilId(fruit.fruitCouncilId) }
.flatMapNone { findByName(fruit.name) }
.flatMap { it.fold({ insert(fruit) }, { id -> update(id, fruit) }) }
@JYCabello
JYCabello / FruitRepository.kt
Last active December 2, 2019 07:32
Functional approach to insertOrUpdate
import arrow.core.None
import arrow.core.Option
import arrow.core.Some
fun insertOrUpdate(fruit: FruitMetadata): Int =
findByAuthorityId(fruit.fruitAuthorityId)
.fold({ findByCouncilId(fruit.fruitCouncilId) }, ::Some)
.fold({ findByName(fruit.name) }, ::Some)
.fold({ insert(fruit) }, { update(it, fruit) })
@JYCabello
JYCabello / FruitRepository.cs
Created December 1, 2019 16:59
Imperative approach to insertOrUpdate
namespace Sandbox.InserOrUpdate
{
public class FruitRepository
{
public int InsertOrUpdate(FruitMetadata fruit)
{
var id = FindByAuthorityId(fruit.FruitAuthorityId) ?? FindByCouncilId(fruit.FruitCouncilId) ?? FindByName(fruit.Name);
return id.HasValue ? Update(id.Value, fruit) : Insert(fruit);
}