Skip to content

Commit dfdb9c8

Browse files
authored
Create 'memoization' shard [MEM-1] (#1)
* Create 'memoization' shard [MEM-1] * Credit Lucky as the starting point for this library * [readme] Use only one h1 and make other headings h2s * Remove "Contributing" and "License" sections I think it's sort of implied that bug reports and pull requests are at least semi-welcome. If they weren't, I could disable the issues and PR tabs. There is a `LICENSE.txt` file and also a `license` field in `shard.yml`. I don't think we also need license info here.
1 parent e27e055 commit dfdb9c8

File tree

12 files changed

+427
-1
lines changed

12 files changed

+427
-1
lines changed

.github/workflows/ci.yml

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
name: Lint & Test
2+
3+
on:
4+
pull_request:
5+
branches:
6+
- '*'
7+
push:
8+
branches:
9+
- main
10+
11+
jobs:
12+
build:
13+
runs-on: ubuntu-latest
14+
steps:
15+
- name: Download source
16+
uses: actions/checkout@v4
17+
18+
- name: Install Crystal
19+
uses: crystal-lang/install-crystal@v1
20+
21+
- name: Cache shards
22+
uses: actions/cache@v4
23+
with:
24+
path: lib
25+
key: ${{ runner.os }}-shards-${{ hashFiles('**/shard.lock') }}
26+
27+
- name: Install shards
28+
run: shards check || shards install
29+
30+
- name: Cache ameba
31+
uses: actions/cache@v4
32+
with:
33+
path: bin/ameba
34+
key: ${{ runner.os }}-ameba-${{ hashFiles('bin/ameba.cr', '**/shard.lock') }}
35+
36+
- name: Force ameba rebuild if needed
37+
run: '[ -f bin/ameba ] || (rm -rf lib/ameba && shards install)'
38+
39+
- name: Run Ameba
40+
run: bin/ameba
41+
42+
- name: Run tests
43+
run: crystal spec

.gitignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
/bin/ameba
2+
/lib/

.runger-config.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
expected-num-github-checks: 1

CHANGELOG.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
## v0.0.1 (2025-03-16)
2+
- Create `memoization` shard.

LICENSE.txt

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
MIT License
2+
3+
Copyright (c) David Runger
4+
5+
Permission is hereby granted, free of charge, to any person obtaining a copy
6+
of this software and associated documentation files (the "Software"), to deal
7+
in the Software without restriction, including without limitation the rights
8+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9+
copies of the Software, and to permit persons to whom the Software is
10+
furnished to do so, subject to the following conditions:
11+
12+
The above copyright notice and this permission notice shall be included in all
13+
copies or substantial portions of the Software.
14+
15+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21+
SOFTWARE.

README.md

Lines changed: 60 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1,60 @@
1-
# memoization
1+
# Memoization
2+
3+
Easy Crystal memoization.
4+
5+
## Installation
6+
7+
Add to your `shard.yml`:
8+
9+
```yml
10+
dependencies:
11+
memoization:
12+
github: davidrunger/memoization
13+
```
14+
15+
## Usage
16+
17+
Just add `memoize` before the method that you would like to memoize.
18+
19+
```crystal
20+
require "memoization"
21+
22+
class Example
23+
memoize def random_number : Float64
24+
Random.rand
25+
end
26+
27+
memoize def hello_with_random_number(name_to_greet : String) : String
28+
"Hello, #{name_to_greet}! #{Random.rand}"
29+
end
30+
31+
def call_private_method
32+
private_method
33+
end
34+
35+
private memoize def private_method : String
36+
"private #{Random.rand}"
37+
end
38+
end
39+
40+
example = Example.new
41+
42+
# Memoize a public method:
43+
pp! example.random_number # => 0.7239151427874982
44+
pp! example.random_number # => 0.7239151427874982
45+
46+
# Memoize a method that takes argument(s):
47+
pp! example.hello_with_random_number("Jane") # => "Hello, Jane! 0.18170318331241622"
48+
pp! example.hello_with_random_number("John") # => "Hello, John! 0.8370694373403952"
49+
pp! example.hello_with_random_number("Jane") # => "Hello, Jane! 0.18170318331241622"
50+
51+
# Memoize a private method:
52+
pp! example.call_private_method # => "private 0.5261598848669066"
53+
pp! example.call_private_method # => "private 0.5261598848669066"
54+
```
55+
56+
## Credit
57+
58+
This library is based on the [Lucky web framework's memoization code][lucky-memoization].
59+
60+
[lucky-memoization]: https://github.com/luckyframework/lucky/blob/v1.3.0/spec/lucky/memoize_spec.cr

bin/ameba.cr

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
# Require ameba cli which starts the inspection.
2+
require "ameba/cli"
3+
4+
# Require ameba extensions here which are added as project dependencies.
5+
# Example:
6+
#
7+
# require "ameba-performance"

bin/githooks/pre-push

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
#!/usr/bin/env bash
2+
3+
set -euo pipefail # exit on any error, don't allow undefined variables, pipes don't swallow errors
4+
5+
lint gitleaks
6+
7+
background-and-notify lint newlines

shard.lock

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
version: 2.0
2+
shards:
3+
ameba:
4+
git: https://github.com/crystal-ameba/ameba.git
5+
version: 1.6.4
6+

shard.yml

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
name: memoization
2+
version: 0.0.1
3+
4+
development_dependencies:
5+
ameba:
6+
github: crystal-ameba/ameba
7+
8+
license: MIT

0 commit comments

Comments
 (0)