no_sltd 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: d65fa0f57aa1ac91c26b613bca068bd6cc4e2154
4
+ data.tar.gz: 12deadde6d42302b9ead771df100fd279ac31d77
5
+ SHA512:
6
+ metadata.gz: f08747e36f3f3b1cacc2bdae42c64b10a6b72ccae9ab7c5cfd405ef57f18cdc24599a649b28279b60bef52172560ae90210e53eb9db80a23de667543ce67859d
7
+ data.tar.gz: 52b7f4af1e8cb7626d82c194cbeacc1a51732625113161111eff16f876e16b313b9315d7670bac641a8defe5d937b0b1d9e7abbf288686291c5ab3e54febef71
data/.gitignore ADDED
@@ -0,0 +1,9 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /Gemfile.lock
4
+ /_yardoc/
5
+ /coverage/
6
+ /doc/
7
+ /pkg/
8
+ /spec/reports/
9
+ /tmp/
data/.travis.yml ADDED
@@ -0,0 +1,8 @@
1
+ sudo: false
2
+ language: ruby
3
+ rvm:
4
+ - 2.1.0
5
+ - 2.2.0
6
+ - 2.3.0
7
+ - 2.4.0
8
+ before_install: gem install bundler -v 1.13.7
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in no_sltd.gemspec
4
+ gemspec
data/LICENSE.txt ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2017 tompng
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
13
+ all 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
21
+ THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,110 @@
1
+ # No Stack Level Too Deep
2
+
3
+ [![Build Status](https://travis-ci.org/tompng/no_sltd.svg?branch=master)](https://travis-ci.org/tompng/no_sltd)
4
+
5
+ Simple way to avoid `stack level too deep`
6
+
7
+ ## Installation
8
+
9
+ ```ruby
10
+ # Gemfile
11
+ gem 'no_sltd'
12
+ ```
13
+
14
+ ```sh
15
+ $ gem install no_sltd
16
+ ```
17
+
18
+ ## Usage
19
+
20
+ ```ruby
21
+ def my_recursive_func
22
+ ...
23
+ end
24
+
25
+ # ↓ just add `no_sltd` before `def`
26
+
27
+ require 'no_sltd'
28
+ no_sltd def my_recursive_func
29
+ ...
30
+ end
31
+ ```
32
+
33
+ or
34
+
35
+ ```ruby
36
+ def my_recursive_func
37
+ ...
38
+ my_recursive_func
39
+ ...
40
+ end
41
+
42
+ # ↓ wrap the recursive function call with `no_sltd { }`
43
+
44
+ require 'no_sltd'
45
+ def my_recursive_func
46
+ ...
47
+ no_sltd { my_recursive_func }
48
+ ...
49
+ end
50
+ ```
51
+
52
+ ### examples
53
+
54
+ ```ruby
55
+ def sum_up n
56
+ return 1 if n == 1
57
+ sum_up(n-1) + n
58
+ end
59
+ sum_up 100000 #=> stack level too deep
60
+
61
+ # ↓↓↓↓
62
+
63
+ no_sltd def sum_up n
64
+ return 1 if n == 1
65
+ sum_up(n-1) + n
66
+ end
67
+ sum_up 100000 #=> 5000050000
68
+ ```
69
+
70
+ ```ruby
71
+ def fibonacci a, memo={0 => 0, 1 => 1}
72
+ return memo[a] if memo[a]
73
+ memo[a] = fibonacci(a-1, memo) + fibonacci(a-2, memo)
74
+ end
75
+ fibonacci 100000 #=> stack level too deep
76
+
77
+ # ↓↓↓↓
78
+
79
+ no_sltd def fibonacci a, memo={0 => 0, 1 => 1}
80
+ return memo[a] if memo[a]
81
+ memo[a] = fibonacci(a-1, memo) + fibonacci(a-2, memo)
82
+ end
83
+ fibonacci 100000 #=> 2597406934722172......
84
+ ```
85
+
86
+ ```ruby
87
+ fact = lambda do |n|
88
+ n.zero? ? 1 : n * fact.call(n-1)
89
+ end
90
+ fact.call 10000 #=> stack level too deep
91
+
92
+ # pass the proc to `no_sltd`
93
+ fact = no_sltd ->(n) {
94
+ n.zero? ? 1 : n * fact.call(n-1)
95
+ }
96
+ fact.call 10000 #=> 2846259680917054......
97
+
98
+ # or wrap the recursive call with `no_sltd { }`
99
+ fact = lambda do |n|
100
+ n.zero? ? 1 : n * no_sltd { fact.call(n-1) }
101
+ end
102
+ fact.call 10000 #=> 2846259680917054......
103
+ ```
104
+
105
+ ### Pros
106
+ - simple & easy
107
+
108
+ ### Cons
109
+ - memory eater
110
+ - slow
data/Rakefile ADDED
@@ -0,0 +1,10 @@
1
+ require "bundler/gem_tasks"
2
+ require "rake/testtask"
3
+
4
+ Rake::TestTask.new(:test) do |t|
5
+ t.libs << "test"
6
+ t.libs << "lib"
7
+ t.test_files = FileList['test/**/*_test.rb']
8
+ end
9
+
10
+ task :default => :test
data/bin/console ADDED
@@ -0,0 +1,14 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require "bundler/setup"
4
+ require "no_sltd"
5
+
6
+ # You can add fixtures and/or initialization code here to make experimenting
7
+ # with your gem easier. You can also use a different console, if you like.
8
+
9
+ # (If you use this, don't forget to add pry to your Gemfile!)
10
+ # require "pry"
11
+ # Pry.start
12
+
13
+ require "irb"
14
+ IRB.start
data/bin/setup ADDED
@@ -0,0 +1,8 @@
1
+ #!/usr/bin/env bash
2
+ set -euo pipefail
3
+ IFS=$'\n\t'
4
+ set -vx
5
+
6
+ bundle install
7
+
8
+ # Do any other automated setup that you need to do here
@@ -0,0 +1,3 @@
1
+ module NoSLTD
2
+ VERSION = "0.1.0"
3
+ end
data/lib/no_sltd.rb ADDED
@@ -0,0 +1,81 @@
1
+ require "no_sltd/version"
2
+
3
+ module NoSLTD
4
+ CONTINUE = Object.new
5
+ THREAD_LOCAL_KEY = :no_sltd_runner
6
+
7
+ class Runner
8
+ attr_reader :result
9
+ def initialize
10
+ @fibers = []
11
+ @stack_level = 0
12
+ end
13
+
14
+ def << fiber
15
+ @fibers << fiber
16
+ end
17
+
18
+ def start &block
19
+ self << Fiber.new { block.call }
20
+ until @fibers.empty? do
21
+ f = @fibers.last
22
+ next unless f
23
+ res = f.resume
24
+ if res != CONTINUE
25
+ @result = res
26
+ @fibers.pop
27
+ end
28
+ end
29
+ @result
30
+ end
31
+
32
+ def direct_callable?
33
+ if @stack_level < 64
34
+ @stack_level += 1
35
+ true
36
+ else
37
+ @stack_level = 0
38
+ false
39
+ end
40
+ end
41
+
42
+ def self.execute &block
43
+ runner = Runner.new
44
+ Thread.current.thread_variable_set(THREAD_LOCAL_KEY, runner)
45
+ runner.start(&block)
46
+ ensure
47
+ Thread.current.thread_variable_set(THREAD_LOCAL_KEY, nil)
48
+ end
49
+
50
+ def self.current
51
+ Thread.current.thread_variable_get(THREAD_LOCAL_KEY)
52
+ end
53
+ end
54
+
55
+ def self.recursive &block
56
+ runner = Runner.current
57
+ return Runner.execute(&block) unless runner
58
+ return yield if runner.direct_callable?
59
+ runner << Fiber.new(&block)
60
+ Fiber.yield CONTINUE
61
+ runner.result
62
+ end
63
+ end
64
+
65
+ def no_sltd method_or_proc=nil, &block
66
+ raise '`no_sltd def func end`, `block = no_sltd -> {}` or `no_sltd { block.call }`' unless block_given? ^ !!method_or_proc
67
+ if block_given?
68
+ NoSLTD.recursive(&block)
69
+ elsif Proc === method_or_proc
70
+ lambda do |*a, &b|
71
+ NoSLTD.recursive { method_or_proc.call(*a, &b) }
72
+ end
73
+ else
74
+ receiver = respond_to?(:instance_method) ? self : self.class
75
+ original = receiver.instance_method method_or_proc
76
+ receiver.send :remove_method, method_or_proc
77
+ receiver.send :define_method, method_or_proc do |*a, &b|
78
+ NoSLTD.recursive { original.bind(self).call(*a, &b) }
79
+ end
80
+ end
81
+ end
data/no_sltd.gemspec ADDED
@@ -0,0 +1,28 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'no_sltd/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "no_sltd"
8
+ spec.version = NoSLTD::VERSION
9
+ spec.authors = ["tompng"]
10
+ spec.email = ["tomoyapenguin@gmail.com"]
11
+
12
+ spec.summary = %q{Simple way to avoid stack level too deep}
13
+ spec.description = %q{Provides a simple way to avoid stack level too deep for recursive functions}
14
+ spec.homepage = "https://github.com/tompng/no_sltd"
15
+ spec.license = "MIT"
16
+ spec.required_ruby_version = '>= 2.1.0'
17
+
18
+ spec.files = `git ls-files -z`.split("\x0").reject do |f|
19
+ f.match(%r{^(test|spec|features)/})
20
+ end
21
+ spec.bindir = "exe"
22
+ spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
23
+ spec.require_paths = ["lib"]
24
+
25
+ spec.add_development_dependency "bundler", "~> 1.13"
26
+ spec.add_development_dependency "rake", "~> 10.0"
27
+ spec.add_development_dependency "minitest", "~> 5.0"
28
+ end
metadata ADDED
@@ -0,0 +1,97 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: no_sltd
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - tompng
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2017-03-07 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: bundler
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: '1.13'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '1.13'
27
+ - !ruby/object:Gem::Dependency
28
+ name: rake
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: '10.0'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '10.0'
41
+ - !ruby/object:Gem::Dependency
42
+ name: minitest
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - "~>"
46
+ - !ruby/object:Gem::Version
47
+ version: '5.0'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: '5.0'
55
+ description: Provides a simple way to avoid stack level too deep for recursive functions
56
+ email:
57
+ - tomoyapenguin@gmail.com
58
+ executables: []
59
+ extensions: []
60
+ extra_rdoc_files: []
61
+ files:
62
+ - ".gitignore"
63
+ - ".travis.yml"
64
+ - Gemfile
65
+ - LICENSE.txt
66
+ - README.md
67
+ - Rakefile
68
+ - bin/console
69
+ - bin/setup
70
+ - lib/no_sltd.rb
71
+ - lib/no_sltd/version.rb
72
+ - no_sltd.gemspec
73
+ homepage: https://github.com/tompng/no_sltd
74
+ licenses:
75
+ - MIT
76
+ metadata: {}
77
+ post_install_message:
78
+ rdoc_options: []
79
+ require_paths:
80
+ - lib
81
+ required_ruby_version: !ruby/object:Gem::Requirement
82
+ requirements:
83
+ - - ">="
84
+ - !ruby/object:Gem::Version
85
+ version: 2.1.0
86
+ required_rubygems_version: !ruby/object:Gem::Requirement
87
+ requirements:
88
+ - - ">="
89
+ - !ruby/object:Gem::Version
90
+ version: '0'
91
+ requirements: []
92
+ rubyforge_project:
93
+ rubygems_version: 2.6.8
94
+ signing_key:
95
+ specification_version: 4
96
+ summary: Simple way to avoid stack level too deep
97
+ test_files: []