Skip to content

Instantly share code, notes, and snippets.

Diogo Kersting diogovk

Block or report user

Report or block diogovk

Hide content and notifications from this user.

Learn more about blocking users

Contact Support about this user’s behavior.

Learn more about reporting abuse

Report abuse
View GitHub Profile
View open_close vs keep_open.cpp
// program_start
fd_log = open("logfile.log")
fn log(msg) {
fd_log.write(msg)
fd_log.flush()
}
@diogovk
diogovk / get_by.ex
Last active May 25, 2016
mini select implementation
View get_by.ex
def get_by(module, params) do
Enum.find all(module), fn map ->
Enum.all?(params, fn {key, val} -> Map.get(map, key) == val end)
end
end
@diogovk
diogovk / Zip and Streams demonstration.ex
Created May 12, 2016
Zip and Streams demonstration
View Zip and Streams demonstration.ex
iex(1)> jump=4
4
iex(2)> s = Stream.unfold(0, fn x -> {x, x+jump} end)
#Function<1.133682015/2 in Stream.unfold/2>
iex(3)> items = [ "aba", "cate", "dood"]
["aba", "cate", "dood"]
iex(4)> Enum.zip(s, items)
[{0, "aba"}, {4, "cate"}, {8, "dood"}]
View How to make multiple (indepedent) pull requests
in master:
# create and checkout new branch
git checkout -b my_feature_1
# work
<my_feature_1> git commit
# I'm ready to push feature_1
# check which remote you want to push to (in exemple below, origin)
View test.exs
defp sign_in(%User{password_digest: password_digest, id: user_id}, password, conn) do
if checkpw(password, password_digest) do
conn
|> put_session(:current_user, user_id)
|> put_flash(:info, "Sign in successful!")
|> redirect(to: page_path(conn, :index))
else
sign_in(nil, nil, conn)
end
end
@diogovk
diogovk / phoenix_belongs_to_catch
Last active Nov 30, 2018
protocol Ecto.Queryable not implemented for <Module>, the given module does not exist
View phoenix_belongs_to_catch
defmodule Freshcards.Post do
use Freshcards.Web, :model
schema "posts" do
field :title, :string
belongs_to :user, Freshcards.User # right!
# belongs_to :user, User <-- wrong
timestamps
end
View elixir_no_currying
defmodule M do
def add(x, y) do
x + y
end
end
1 |> M.add(2) |> IO.puts #=> 3
# add1 = 1|> add #=> illegal undefined function add/1
# Em certas linguagens isto faria um currying que é uma aplicação parcial de 1 na função add
View Grunt versus makefile
### Grunt Dependencias ###
"envify": "~1.2.1",
"grunt": "~0.4.4",
"grunt-browserify": "~1.3.2",
"grunt-contrib-watch": "~0.6.1",
"grunt-env": "~0.4.1"
#Note que não faz o uglify!
#### Grunt ####
var fs = require('fs');
View inimplementation.ex
defmodule Stuff do
def inlist([search_item|_tail], search_item), do: true
def inlist([_head|tail], search_item) , do: inlist(tail, search_item)
def inlist([], _), do: false
end
IO.inspect(Stuff.inlist([1,2,3],1))
View gist:4bca024e23c9a715c41b
defmodule ListOps do
# Please don't use any external modules (especially List) in your
# implementation. The point of this exercise is to create these basic functions
# yourself.
#
# Note that `++` is a function from an external module (Kernel, which is
# automatically important`) and so shouldn't be used either.
@spec count(list) :: non_neg_integer
You can’t perform that action at this time.