Skip to content

Instantly share code, notes, and snippets.

View ikzekly's full-sized avatar
💋

ilia ikzekly

💋
View GitHub Profile
pub fn nth(n: u32) -> Option<u32> {
pub fn is_prime(n: u32) -> bool {
if n == 1 {return false};
if n == 2 {return true};
let sqrt_n = ((n as f32).sqrt() as u32) + 1;
for i in 2..sqrt_n {
if n % i == 0 {return false};
}
true
@ikzekly
ikzekly / rails_count-of-assotiation
Last active September 2, 2018 20:24
Take all users, who having five and less invoices
User.joins('LEFT JOIN "invoices" ON "invoices"."merchant_id" = "users"."id"').group("users.id").having("count(*) <= ?", 5)
WITH messages_ranks AS (
SELECT *,
rank() OVER ( PARTITION BY channel_id ORDER BY created_at DESC) AS rank
FROM messages
)
SELECT row_to_json(t)
FROM (
SELECT channels.id,
channels.name,
channels.owner_id,
@ikzekly
ikzekly / sort_by_sequence.rb
Created September 23, 2019 14:00
Sorting array by sequences and alphabetically
sequence1 = %w[d c b]
sequence2 = %w[e a f]
base = %w[z a c d f y b]
def sort_by_sequence(sequence:)
array = yield
array.select { |el| sequence.include?(el) }.sort do |left, right|
sequence.index(left) <=> sequence.index(right)
end | sort_by_alfabet { array }
end
@ikzekly
ikzekly / temporary_lock.rb
Created August 4, 2022 17:17
Provides Redis distributed lock api
# frozen_string_literal: true
# @example provides Redis distributed lock api
#
# $ TemporaryLock.create!(:example, 'c22a0420-51c8-4e47-843c-92a37e8d2eba')
# => #<TemporaryLock:0x000055b43e328410 @key="temporary_lock:example:c22a0420-51c8-4e47-843c-92a37e8d2eba">
#
# @example raises `TemporaryLock::StaleObjectError` when `id` already registered
#
# $ TemporaryLock.create!(:example, 'c22a0420-51c8-4e47-843c-92a37e8d2eba')