Skip to content

Instantly share code, notes, and snippets.

@harikrishnan83
harikrishnan83 / KotlinRailwayOrientedProgrammingWithArrow.kt
Last active June 14, 2020 06:46
Kotlin Railway Oriented Programming with Arrow and Infix
import arrow.core.Either
import arrow.core.flatMap
sealed class Result<T>
data class Success<T>(val message: T) : Result<T>()
data class Failure<T>(val errorMessage: T) : Result<T>()
infix fun <F, S> Either<F, S>.then(f: (S) -> Either<F, S>) =
this.flatMap { f(it) }
sealed class Result<T>
data class Success<T>(val message: T): Result<T>()
data class Failure<T>(val errorMessage: String): Result<T>()
class HttpRequest(private val url: String, private val method: HttpMethod, private val body: String) {
fun matches(anotherRequest: HttpRequest): Result<HttpRequest> {
return when (val matchUrlResult = matchUrl(anotherRequest)) {
is Failure -> matchUrlResult
is Success -> {
when (val matchMethodResult = matchMethod(anotherRequest)) {
class HttpRequest(private val url: String, private val method: HttpMethod, private val body: String) {
fun matches(anotherRequest: HttpRequest): Result {
when (val result = matchUrl(anotherRequest)) {
is Failure -> return result
}
when (val result = matchMethod(anotherRequest)) {
is Failure -> return result
}
when (val result = matchBody(anotherRequest)) {
is Failure -> return result
class HttpRequest(private val url: String, private val method: HttpMethod, private val body: String) {
fun matches(anotherRequest: HttpRequest): Result {
if (this.url != anotherRequest.url)
return Failure("URL did not match. ${this.url} not equal to ${anotherRequest.url}")
if (this.method != anotherRequest.method)
return Failure("Method did not match. ${this.method} not equal to ${anotherRequest.method}")
if (this.body != anotherRequest.body)
return Failure("Body did not match. ${this.body} not equal to ${anotherRequest.body}")
return Success("everything matches")
}
@harikrishnan83
harikrishnan83 / RailwayOrientedProgramming.kt
Last active February 27, 2020 18:27
Kotlin Railway Oriented Programming
//Kotlin Railway Oriented Programming
sealed class Result<T>
data class Success<T>(val message: T) : Result<T>()
data class Failure<T>(val errorMessage: String) : Result<T>()
infix fun <T, U> Result<T>.then(f: (T) -> Result<U>) =
when (this) {
is Success -> f(this.message)
is Failure -> Failure(this.errorMessage)
@harikrishnan83
harikrishnan83 / convert_all_pages_to_pdf.rb
Last active August 29, 2015 14:02
Convert all pages with Carrierwave::MiniMagick
# MiniMagick::Image.format method by default converts only the first page.
# Carrierwave::MiniMagick.convert(:pdf) does not allow us to pass a second parameter page = nil.
# We need to paste in below code in the carrierwave initializer (or you may choose to extract it to lib folder)
# and use convert_all_pages_to_pdf in you uploader.
module CarrierWave
module MiniMagick
def convert_all_pages_to_pdf
manipulate! do |img|
img.format('pdf', nil)
@harikrishnan83
harikrishnan83 / gist:6578323
Last active December 23, 2015 04:09
Given latitude and longitude get the city in ruby using Geokit gem
#Put below line in an initializer script
Geokit::Geocoders::GOOGLE="<Your api key>"
Geokit::Geocoders::GoogleGeocoder3.reverse_geocode([12.9898001,77.7234591])
/**
* An example to illustrate the CyclicBarrier in java.util.concurrent package.
* Here we are creating a barrier to an entry for a coding session.
* A minimum of three programmers are necessary to start the coding session.
*/
import java.util.concurrent.BrokenBarrierException;
import java.util.concurrent.CyclicBarrier;
public class CodingDojo {