Skip to content

Instantly share code, notes, and snippets.

View vitaly-pushkar's full-sized avatar

Vitaly Pushkar vitaly-pushkar

View GitHub Profile
# Enable Powerlevel10k instant prompt. Should stay close to the top of ~/.zshrc.
# Initialization code that may require console input (password prompts, [y/n]
# confirmations, etc.) must go above this block; everything else may go below.
if [[ -r "${XDG_CACHE_HOME:-$HOME/.cache}/p10k-instant-prompt-${(%):-%n}.zsh" ]]; then
source "${XDG_CACHE_HOME:-$HOME/.cache}/p10k-instant-prompt-${(%):-%n}.zsh"
fi
source "${HOME}/.zgen/zgen.zsh"
#if the init script doesn't exist
@vitaly-pushkar
vitaly-pushkar / endpoint.rb
Last active January 31, 2020 21:22
From the blog post "Error Handling with Either Monads in Ruby"
patch '/users/:id/' do
result = UserUpdater.new(User, config.mailer).call(params).to_result
# a simple, "unambitious" version
if result.success?
result.sucess.to_json
else
{error: result.failure}.to_json
end
end
@vitaly-pushkar
vitaly-pushkar / try_monad.rb
Created January 23, 2020 20:06
From the blog post "Error Handling with Either Monads in Ruby"
def find_user(id)
Try { user_model.find_by(id: id) }
end
def update_user(user, data)
Try do
user.update(data)
user.reload
end
end
@vitaly-pushkar
vitaly-pushkar / do_notation.rb
Created January 23, 2020 20:05
From the blog post "Error Handling with Either Monads in Ruby"
def call(fields)
fields = yield validate_fields(fields)
email = yield validate_email(fields['email'])
user = yield find_user(fields['id'])
user = yield update_user(user, {name: fields['name'], email: fields['email']})
sent = yield send_email(user, :profile_updated)
Success(user)
end
@vitaly-pushkar
vitaly-pushkar / db_setup.rb
Created January 23, 2020 20:04
From the blog post "Error Handling with Either Monads in Ruby"
ActiveRecord::Base.establish_connection( adapter: 'sqlite3', database: ":memory:" )
ActiveRecord::Schema.define(version: 1) { create_table(:users) { |t| t.string :name; t.string :email } }
class User < ActiveRecord::Base; end
User.create(name: 'Vitaly', email: 'vitaly.pushkar@gmail.com')
@vitaly-pushkar
vitaly-pushkar / either_example.rb
Last active November 27, 2020 05:13
From the blog post "Error Handling with Either Monads in Ruby"
class UserUpdater
include Dry::Monads[:result]
attr_reader :user_model, :mailer
def initialize(user_model, mailer)
@user_model = user_model
@mailer = mailer
end
@vitaly-pushkar
vitaly-pushkar / ChangingMoney.scala
Last active January 11, 2018 09:29
Changing money problem solved recursively in Scala with a greedy approach
import scala.annotation.tailrec
object ChangingMoney extends App {
val money = scala.io.StdIn.readInt()
// canonical coin system; this algorithm will not work optimally for arbitrary coin systems
val coins = List(10, 5, 1)
println(changeMoney(money))

Keybase proof

I hereby claim:

  • I am vitaly-pushkar on github.
  • I am pshk4r (https://keybase.io/pshk4r) on keybase.
  • I have a public key ASA2hL6yQsXIZKVTrWaRn1Htv26UwrFThgrbrZIrwkkzdgo

To claim this, I am signing this object:

@vitaly-pushkar
vitaly-pushkar / test.scala
Last active January 4, 2017 07:14
Excercise 2.2
// implement isSorted, which checks whether an Array[A] is sorted according to a given comparison function:
// def isSorted[A] (as: Array[A], ordered: (A, A) => Boolean): Boolean
object Test {
def isSorted[A](as: Array[A], ordered: (A, A) => Boolean): Boolean = {
def loop(n: Int): Boolean =
if (n+1 >= as.length) true
else if (!ordered(as(n), as(n+1))) false
else loop(n+1)

Keybase proof

I hereby claim:

  • I am vitaly-pushkar on github.
  • I am pshk4r (https://keybase.io/pshk4r) on keybase.
  • I have a public key whose fingerprint is 3E2C 6703 DA33 3DD6 F718 4FF4 22E5 2E81 5D58 FC86

To claim this, I am signing this object: