Skip to content

Instantly share code, notes, and snippets.

🤔
scheming

Jacek Królikowski nietaki

🤔
scheming
Block or report user

Report or block nietaki

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 medium_preload_functions.ex
def category_preloader(category_names) when is_list(category_names) do
# category_names are already deduplicated
Enum.map(category_names, &RemoteService.retrieve_category_info/1)
end
query =
from(p in Post,
where: p.id in ^post_ids,
preload: [category: ^&category_preloader/1]
)
View medium_post_schema_final.ex
defmodule BlogExample.Schema.Category do
use Ecto.Schema
@primary_key {:name, :string, []}
embedded_schema do
field(:current_popularity, :integer)
end
end
View medium_virtual_field_enum_map.ex
query =
from(p in PostWithVirtualCategory,
where: p.id in ^post_ids
)
posts =
query
|> Repo.all()
|> Enum.map(&PostWithVirtualCategory.populate_category/1)
View medium_virtual_example.ex
defmodule BlogExample.Schema.PostWithVirtualCategory do
use Ecto.Schema
schema "posts" do
field(:title, :string, null: false)
field(:contents, :string, null: false)
field(:category_name, :string)
field(:category, :map, virtual: true)
View medium_preload_on_structs.ex
simpler_query = from(c in Comment, where: c.user_id == ^charlie_id)
non_preloaded = Repo.all(simpler_query)
assert Enum.all?(non_preloaded, fn c -> c.post == nil end)
comments_by_charlie = Repo.preload(non_preloaded, [:post])
refute Enum.any?(comments_by_charlie, fn c -> c.post == nil end)
View medium_simple_preload.ex
# aliases and imports omitted
query =
from(c in Comment,
where: c.user_id == ^charlie_id,
preload: [:post]
)
comments_by_charlie = Repo.all(query)
refute Enum.any?(comments_by_charlie, fn c -> c.post == nil end)
View medium_simple_query.ex
alias BlogExample.Schema.Comment
import Ecto.Query, only: [from: 2]
query =
from(c in Comment,
where: c.user_id == ^charlie_id
)
comments_by_charlie = Repo.all(query)
View medium_schemas.ex
defmodule BlogExample.Schema.User do
use Ecto.Schema
schema "users" do
field(:username, :string, null: false)
field(:full_name, :string)
has_many(:posts, BlogExample.Schema.Post)
has_many(:comments, BlogExample.Schema.Comment)
end
View solution.java
public class Solution {
public int solution(int[] radiuses) {
int length = radiuses.length;
int[] startCounts = new int[length];
int[] endCounts = new int[length];
// make sure there's no overflows
for (int i = 0; i < length; i++) {
radiuses[i] = Math.min(length, radiuses[i]);
}
View gist:718849b16348b08b9ae13222f8bc784c
https://my.pcloud.com/#page=puplink&code=4tvZTrwxhAmIOn0mAATwUzeMEXARCcqk
bdbd
You can’t perform that action at this time.