Skip to content

Instantly share code, notes, and snippets.

View leandronsp's full-sized avatar

Leandro Proença leandronsp

  • Brazil
View GitHub Profile
@leandronsp
leandronsp / ruby-functions.rb
Last active January 2, 2016 18:29
ruby the functional way
def flist(list)
clone = list.dup
return [] if list == []
[ clone.shift, clone ]
end
def len(list, acc = 0)
return acc if list == []
_, tail = flist list
len(tail, acc + 1)
class CityDocument < ActiveRecord::Base
include PgSearch
self.table_name = 'city_document_view'
self.primary_key = 'city_id'
pg_search_scope :_not_so_expensive_search_with_coords, (lambda do |term, lat, lon|
{
query: term,
# exact point
3.0 ^ ((0.0 + 1.0) ^ -0.1) => 3.0
# getting longer
3.0 ^ ((0.5 + 1.0) ^ -0.1) => 2.87
3.0 ^ ((100 + 1.0) ^ -0.1) => 1.99
# so far
3.0 ^ ((99999999 + 1.0) ^ -0.1) => 1.19
# city_document_view
def up
execute <<-SQL
CREATE MATERIALIZED VIEW city_document_view AS
SELECT cities.id as city_id,
cities.name as city_name,
cities.population as population,
cities.longitude as longitude,
cities.latitude as latitude,
class City < ActiveRecord::Base
include PgSearch
belongs_to :region
belongs_to :country
has_many :city_aliases
##
# Search against a single field is not so expensive,
##
pg_search_scope :_expensive_search_with_coords, (lambda do |term, lat, lon|
{
query: term,
against: { name: 'A' },
associated_against: {
city_aliases: { name: 'B' },
region: { name: 'C' },
country: { name: 'D' }
},
ignoring: :accents,
City._cheap_search('london') # milliseconds
City._expensive_search('london') # more results, but more expensive (seconds)
City._expensive_search_with_coords('london', 34.54426, -91.96903) # smart, but way expensive (lotsa seconds)
@leandronsp
leandronsp / -RUBY_BLOCKS.md
Last active April 7, 2018 00:28
Ruby blocks made easy

Ruby blocks

Some explanation and examples of Ruby blocks. Encouraged to follow each file in the correct numbered order.

@leandronsp
leandronsp / README.md
Last active April 9, 2018 10:24
Programming made easy®

Programming made easy®

One can say that programming is not for everyone, but truth is we often miss the opportunity to find passionate people on teaching that think out-of-the-box and try to create a common bridge of communication so anyone really interested can smoothly grasp concepts such as:

  • Computer CPU's speak bits
  • Assembly is nice, saves us much time from typing bits for the processor, but it's not portable
  • Programming languages are just specifications and abstractions which make our lives easier. But someone has to implement such specs in an already existing language
  • Type systems made easy®

Keybase proof

I hereby claim:

  • I am leandronsp on github.
  • I am leandronsp (https://keybase.io/leandronsp) on keybase.
  • I have a public key ASBuO-wv2r1vTmLizbyfx3YiPldG8on4B3jknrMFX_JUpQo

To claim this, I am signing this object: