Skip to content

Instantly share code, notes, and snippets.

@mattsan
mattsan / cli.ex
Last active May 25, 2019 12:14
[練習] wc (の機能の一部)を C++ で実装してみた
defmodule ExCount.CLI do
@opts [
strict: [only_line: :boolean],
aliases: [l: :only_line]
]
def main(args) do
args
|> OptionParser.parse(@opts)
|> show_counts()
@mattsan
mattsan / README.md
Created May 19, 2019 00:56
Serverless を使った Ruby の AWS Lambda のプロジェクト例

Serverless を使った Ruby の AWS Lambda のプロジェクト例

ディレクトリ構成

.
├── handler.rb
├── layers/
│   ├── Gemfile
│   └── Gemfile.lock
@mattsan
mattsan / application.js
Created May 10, 2019 14:22
Action Cable + Stimulus
require('@rails/ujs').start()
require('turbolinks').start()
import { Controller, Application } from 'stimulus'
import HomeChannel from './home_channel'
class HomeController extends Controller {
static targets = [
'message'
]
@mattsan
mattsan / app.js
Created May 6, 2019 03:49
Uikit を使った modal と sortable (Drap & Drop) のスパイク
import css from "../css/app.css"
import "phoenix_html"
import UIkit from 'uikit'
document.addEventListener('DOMContentLoaded', function() {
// Uikit のイベントのハンドリング
// Sortable の要素の移動をハンドリング
UIkit.util.on('#chapters', 'moved', (event) => {
console.log(event.detail)
});
@mattsan
mattsan / app.css
Created May 6, 2019 03:44
クリックした場所にDOMが移動する (Phoenix + Stimulusjs)
.field {
background-color: #eee;
height: 90vh;
width: 100%;
}
.marker {
position: relative;
left: 10px;
height: 10px;
@mattsan
mattsan / greeter.ex
Last active May 5, 2019 12:55
Stimulusjs と Channel と Faker を使って 5 秒ごとに挨拶をするサンプル。
defmodule StimulusPhxSampleWeb.Greeter do
use GenServer
def start_link(_) do
GenServer.start_link(__MODULE__, %{})
end
def init(state) do
schedule_greeting()
{:ok, state}
# sample of CSV package
#
# see https://hex.pm/packages/csv
"a,b\n1,2\n3,4\n"
|> IO.inspect()
|> String.split() # split a string into lines
|> IO.inspect()
|> CSV.decode!(headers: [:a, :b]) # decode with headers
|> Enum.to_list() # realize a list of maps
@mattsan
mattsan / conv.ex
Created April 28, 2019 02:39
文字列のエンコーディングの変換
defmodule Conv do
def get(url) do
{:ok, response} = HTTPoison.get(url)
:iconv.convert("SJIS", "UTF-8", response.body)
end
end
@mattsan
mattsan / orde30.prolog
Created February 9, 2019 09:23
オフラインリアルタイムどう書く E30 「面白いセルの合計」を Prolog で解いた
% オフラインリアルタイムどう書く E30 「面白いセルの合計」を Prolog で解いた
% http://nabetani.sakura.ne.jp/hena/orde30sumt/
%
% 述語 memo/3 を動的に定義することで memoization を実現しています
%
% 処理系:
% GNU Prolog (http://www.gprolog.org)
%
% コンパイル:
% $ gplc --no-top-level orde30.prolog
@mattsan
mattsan / liquid_sample.ex
Created February 9, 2019 07:42
A sample of liquid in Elixir
{:ok, greeting, _} =
"{{ greeting | capitalize }}, {{ greeting | downcase }}!"
|> Liquid.Template.parse()
|> Liquid.Template.render(%{"greeting" => "HELLO"})
IO.puts(greeting)
# => Hello, hello!