Skip to content

Instantly share code, notes, and snippets.

View luchiago's full-sized avatar
🚀
Learning

Lucas Hiago luchiago

🚀
Learning
View GitHub Profile
@luchiago
luchiago / curry.rb
Created September 14, 2019 19:38
Currying in ruby
#Allows a function accepts n parameters and turns it into a sequence of n functions, each of them take 1 parameter.
power_function = -> (x, z) {
(x) ** z
}
base = gets.to_i
raise_to_power = power_function.curry.(base)
power = gets.to_i
@luchiago
luchiago / spread.js
Created January 8, 2020 22:52
An use of spread operator in JavaScript
const user = {
name: 'Lucas',
lastName: 'Hiago'
};
function getUserWithFullName(user) {
return {
...user, //spead operator
fullName: `${user.name} ${user.lastName}`
}
@luchiago
luchiago / reduce_and_map.js
Last active January 22, 2020 01:31
An example using reduce and map functions from JavaScript
const students_grades = [
[7.0, 8.0, 6.0],
[10.0, 10.0, 10.0],
[5.0, 9.0, 10.0]
]
const reducer = (exams) => {
let starter = 0
let number_of_exams = 3
@luchiago
luchiago / odd_or_even.py
Created March 5, 2020 17:28
Odd or even lambdas to make more readable
is_even = lambda number: number % 2 == 0
is_odd = lambda number: number % 2 != 0
range = range(1, 11)
# Generate list using list comprehension
even_numbers = [ number for number in range if is_even(number) ]
odd_numbers = [ number for number in range if is_odd(number) ]
# Output
@luchiago
luchiago / active_record_associations.rb
Created March 8, 2020 02:06
Rails preload, eager_load and includes
# Example Models
class Tournament
attribute :name, :string
has_many :teams
end
class Teams
attribute :name, :string
@luchiago
luchiago / pre-commit.bash
Last active April 28, 2020 00:46
Hook for pre-commit to check rubocop and erb problems
#!/usr/bin/env bash
echo "Running pre-commit hook"
fileList_rb=$(git diff --diff-filter=d --cached --name-only | grep -E '\.(rb)$')
fileList_erb=$(git diff --diff-filter=d --cached --name-only | grep -E '\.(erb)$')
if [ ${#fileList_rb} -lt 1 ] && [ ${#fileList_erb} -lt 1 ]; then
echo -e "You have no staged .rb or .erb files to test\n"
exit
@luchiago
luchiago / create_meetings.rb
Last active June 20, 2020 22:55
Migration for multiple foreign keys with the same model in Rails 6
class CreateMeetings < ActiveRecord::Migration[6.0]
def change
create_table :meetings do |t|
t.datetime :starts_at, null: false
t.datetime :ends_at, null: false
t.references :available_user, null: false
t.references :requester_user, null: false
t.timestamps
end
# @param {String} j
# @param {String} s
# @return {Integer}
def num_jewels_in_stones(j, s)
jewels = j.split("")
total = 0
s.split("").each do |stone|
total += 1 if jewels.include?(stone)
end
@luchiago
luchiago / tox.md
Created July 9, 2020 19:38
[BACKEND][PYTHON][DJANGO] Notes about Tox

Tox

According to official website, tox aims to automate and standardize testing in Python. It is part of a larger vision of easing the packaging, testing and release process of Python software.

What is tox?

tox is a generic virtualenv management and test command line tool you can use for:

  • checking your package installs correctly with different Python versions and interpreters
  • running your tests in each of the environments, configuring your test tool of choice
  • acting as a frontend to Continuous Integration servers, greatly reducing boilerplate and merging CI and shell-based testing.

Usage

You can install tox from PyPi using the command pip install tox. After configuring a tox.ini fie you can run tox and see if everything is correct

The config file

@luchiago
luchiago / mixins.md
Created July 9, 2020 23:10
[BACKEND][PYTHON][DJANGO] What are mixins?

Mixins

Reference

Motivation to use Mixins

A way to achieve modularity and reusability in OO is through this concept. Multiple languages implement this in different ways. In Python, mixins are supported via multiple inheritance

Context: Python

Mixin is a parent class that provides functionality to subclasses but is not intended to be instantiated itself. It can have both and concrete and abstract methods that are basically abstract base classes. Mixins can be regarded as a specific strain of abstract base classes where they can house both concrete and abstract methods but don't keep any internal states. These can help you when

  • You want to provide a lot of optional features for a class.
  • You want to provide a lot of not-optional features for a class, but you want the features in separate classes so that each of them is about one feature (behavior).
  • You want to use one particular feature in many different