This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
defmodule Testing do | |
""" | |
You can pipe straight into a case | |
Kernel.<<>>, the special form for binary pattern | |
matching can be used in a case match to | |
split up incoming binaries, like this | |
imaginary |~ delimited protocol. | |
"fiexd_size_header|~some message" | |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@doc """ | |
You can pipe queries into this build_where/3 | |
fn and match on operators like :in, :==, | |
:ilike, etc and handle the particular | |
where semantics in each function case. | |
Pay attention to the positional binding | |
[...,e] which will capture the last | |
binding added to the piped in query. | |
See also: https://hexdocs.pm/ecto/Ecto.Query.html#module-positional-bindings | |
and. https://hexdocs.pm/ecto/Ecto.Query.API.html#field/2 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from(c in City, select: c) # returns the schema as a struct | |
from(c in City, select: {c.name, c.population}) # returns Tuple of {name, population} | |
from(c in City, select: [c.name, c.county]) # returns List of [name, county] | |
from(c in City, select: %{n: c.name, answer: 42}) # returns Map with keys :n, and :answer | |
from(c in City, select: %{c | alternative_name: c.name}) # returns the schema as a Struct with :alternative_name added | |
from(c in City, select: %Data{name: c.name}) # returns the selected name in %Data{} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
defmodule MyApi.Schema.Pipeline.MyCompilePhase do | |
@moduledoc """ | |
based on Absinthe phases https://hexdocs.pm/absinthe/1.5.3/Absinthe.Phase.html | |
""" | |
alias Absinthe.{Blueprint, Pipeline, Phase} | |
alias Absinthe.Blueprint.Schema.{ObjectTypeDefinition, FieldDefinition} | |
def pipeline(pipeline) do | |
Pipeline.insert_after(pipeline, Phase.Schema.TypeImports, __MODULE__) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
file_changes="$(git diff --name-only $(git merge-base master HEAD))" | |
migration_files="$(echo $file_changes | grep -c /migrations/)" | |
application_files="$(echo $file_changes | grep -c -v /migrations/ --include \*.ex --include \*.exs)" | |
echo "checking for migrations and apllication changes" | |
if (($migration_files == 0)) && (($application_files > 0)); | |
then | |
echo "Only Application Changes. OK" | |
exit 0 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
defmodule Boggle do | |
def full_match(dictionary, word) do | |
case Enum.member?(dictionary, word) do | |
true -> word | |
false -> nil | |
end | |
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
namespace :json do | |
desc 'Export all data to JSON files' | |
task :export => :environment do | |
Rails.application.eager_load! | |
ApplicationRecord.descendants.each do |model| | |
next if model.table_name.nil? || model.table_name == '' | |
begin | |
data = model.all | |
next if data == [] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class DoubleStack | |
attr_accessor :inbox, :outbox | |
def initialize | |
@inbox = [] | |
@outbox = [] | |
end | |
def enqueue(item) | |
@inbox << item | |
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
################################# | |
# Top-down recursive merge sort | |
################################# | |
################################# | |
# Worst-case perf O(n log n) | |
# Bestt-case perf O(n log n) | |
# Worst-case space complexity O(n) | |
################################# |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def find_suffix(collection, query_string) | |
# Inject 0 so that when evaluating the block, you have | |
# and object that responds to `+` | |
# if the array element is equal to the query_string | |
# add to the sum and return the sum to be used in | |
# the next step | |
collection.inject(0) { |sum, str| sum += 1 if str == query_string; sum } | |
end |
NewerOlder