Skip to content

Instantly share code, notes, and snippets.

View hoangbits's full-sized avatar
🏝️
Working from earth

Le Gia Hoang hoangbits

🏝️
Working from earth
  • Thanh Hoa, Vietnam
  • 13:55 (UTC +07:00)
View GitHub Profile
// *********** START DE QUY **************
//
// tuong tuong co nhieu ham add
//
// function add(number){
// if(number <= 0) {
// return 0;
// }
let age = 12
let value = `age`
let value = `${age}`
irb(main):005:0> (1..5).include?(5) #=> true
=> true
irb(main):006:0> (1...5).include?(5) #=> false
=> false
irb(main):007:0> 1.upto(5).include?(5)
=> true
# =begin
# Mã vạch (ISBN-10) là 1 chuỗi gồm 9 chữ số (0-9) và 1 ký tự kiểm tra (ký tự này có thể là 1 chữ
# số hoặc chữ X). Trường hợp ký tự kiểm tra là X thì nó có giá trị bằng 10.
# Các ký tự có thể nối với nhau bằng dấu gạch nối '-', hoặc không có gạch nối.
# VD: 3-598-21508-8, 3-598-21507-X, 3598215088
# Kiểm tra tính hợp lệ của mã vạch bằng công thức:
# (x1 * 10 + x2 * 9 + x3 * 8 + x4 * 7 + x5 * 6 + x6 * 5 + x7 * 4 + x8 * 3 + x9 * 2 + x10 * 1) mod 11 == 0
# Bằng 0 thì hợp lệ. Trong đó x1->x10 lần lượt là các số từ trái sang phải (không bao gồm gạch nối)
# VD mã vạch 3-598-21508-8 hợp lệ vì
# (3 * 10 + 5 * 9 + 9 * 8 + 8 * 7 + 2 * 6 + 1 * 5 + 5 * 4 + 0 * 3 + 8 * 2 + 8 * 1) mod 11 == 0
irb(main):015:0> ["1",2,3,4,5].select {|val| val.is_a? Numeric }
=> [2, 3, 4, 5]
irb(main):016:0> ["X","-",3,4,5].select {|val| val.is_a? Numeric or val == "X"}
=> ["X", 3, 4, 5]
dfdfdfd
@hoangbits
hoangbits / model.ex
Created April 12, 2022 08:51
implicit encode Ecto schema with Jason
# inside an ecto Schema, adding this:
defimpl Jason.Encoder, for: [__MODULE__] do
def encode(struct, opts) do
Enum.reduce(Map.from_struct(struct), %{}, fn
({k, %Ecto.Association.NotLoaded{}}, acc) -> acc
({k, v}, acc) -> Map.reject(acc, fn {k, v} -> k === :__meta__ end) |> Map.put(k, v)
end)
|> Jason.Encode.map(opts)
end
@hoangbits
hoangbits / snippets.json
Last active September 20, 2021 11:11
Liveview snippets for VS-code
//
// To install these snippets, select "User Snippets" under "File >
// Preferences" ("Code > Preferences" on macOS), and then select
// elixir.json. Paste the snippets below into that file. If you already
// have snippets in that file, you'll need to put all the snippets in one
// top-level JavaScript object, so remove the outer braces in the code
// below.
//
// JSON doesn't allow comments, so you must remove these comment lines! :-)
//
@hoangbits
hoangbits / catch-promise
Last active September 13, 2021 09:24
handle UnhandledPromiseRejection
/**
Ref: https://jonasjancarik.medium.com/handling-those-unhandled-promise-rejections-when-using-javascript-async-await-and-ifee-5bac52a0b29f
Placement of catch BEFORE and AFTER then: https://stackoverflow.com/questions/42013104/placement-of-catch-before-and-after-then
*/
// 1. Work
(async()=>{
return Promise.reject("a").catch((_error) =>{})
defmodule ModuleToBeUsed do
defmacro __using__(_) do
IO.puts __MODULE__ # COMPILATION STAGE
quote do # needed to prevent execution on compilation stage
IO.inspect(__MODULE__, label: "__MODULE__")
IO.inspect(unquote(__MODULE__), label: "unquote(__MODULE__)")
import unquote(__MODULE__)
def test, do: IO.puts "I am test" # EXECUTION STAGE
end
end