flex-cartesian 1.2.2 → 1.3.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (5) hide show
  1. checksums.yaml +4 -4
  2. data/CHANGELOG.md +4 -0
  3. data/README.md +11 -1
  4. data/lib/flex-cartesian.rb +35 -7
  5. metadata +2 -2
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 3a2ca7796b432b25c13982324959267a48475d5e9bce840b36309f976e05aa8f
4
- data.tar.gz: c43b4a80cda5177c29cd7e07c186d6075fa2597ed3fe3e4e886e49b94b76ef49
3
+ metadata.gz: 20aebfbce656dcb5bbe2467cae65ad8bc510022f6a908b88f55f14101a92e1c3
4
+ data.tar.gz: 334d472b4cd56b2a159459c2a8f46d80c704780de7ff348fe711a63d7930c1dc
5
5
  SHA512:
6
- metadata.gz: 8271368f575fdd9dc460d6acb7d37678faf8071293738c9cf421ad7fa3dbc0bcf197590d79e57216cc20ee4b028ffa0b58f84b160dddb900b71d778ba93ef8e8
7
- data.tar.gz: 4e55180b78619c9ad100c0b6e7db1b19cc7c74871936939340d53af7b6e01eba82bd2bd2e739e0e33c3ef334cfb66f75284aeae1ef2ca1dcebbe00f60eb5843a
6
+ metadata.gz: f865d4ad80b7ff10d85ebbeaf1a0a2e0185fe9798cf45dd50c1ae4246ad1dd96cf3b01983c29606f27886312335c4aa7e540b45f38e25c37753df7ce5bfbe152
7
+ data.tar.gz: 9250f059eeb325d2d69732af18207561351cb8e86d3a6da878b1d66ab37e1d39f970af8385a463aa5ef89d0261f9cd7eda7eac39648c492c70e7bf841d181028
data/CHANGELOG.md CHANGED
@@ -1,5 +1,9 @@
1
1
  # Changelog
2
2
 
3
+ ## 1.3.0 - 2025-08-19
4
+ ### Added
5
+ - .func accepts flag order:
6
+
3
7
  ## 1.2.2 - 2025-07-28
4
8
  ### Added
5
9
  - .dimensions functionality extended to more granular formatting
data/README.md CHANGED
@@ -179,7 +179,16 @@ puts "\Add and then remove function 'test'"
179
179
  s.func(:add, :test) { |v| v.dim3.to_i }
180
180
  s.func(:del, :test)
181
181
 
182
+ puts "\nThis function will calculate first, always"
183
+ s.func(:add, :test_first, order: :first) { puts "HERE" }
182
184
 
185
+ puts "\nThis function will calculate last, always"
186
+ s.func(:add, :test_last, order: :last) { puts "HERE" }
187
+
188
+ puts "\nNew functions respect first and last ones"
189
+ s.func(:add, :test_more) { puts "HERE" }
190
+
191
+ puts "\nFirst and last functions are handy for pre- and post-processing for each combination"
183
192
 
184
193
  # CONDITIONS ON CARTESIAN SPACE
185
194
 
@@ -377,7 +386,7 @@ s.cartesian { |v| puts "#{v.dim1} - #{v.dim2}" }
377
386
 
378
387
  ### Handling Functions
379
388
  ```ruby
380
- func(command = :print, name = nil, hide: false, progress: false, title: "calculating functions", &block)
389
+ func(command = :print, name = nil, hide: false, progress: false, title: "calculating functions", order: nil, &block)
381
390
  ```
382
391
  - `command`: symbol, one of the following
383
392
  - `:add` to add function as a virtual dimension to Cartesian space
@@ -388,6 +397,7 @@ func(command = :print, name = nil, hide: false, progress: false, title: "calcula
388
397
  - `hide`: flag that hides or shows the function in .output; it is useful to hide intermediate calculations
389
398
  - `progress`: show progress bar during `:run`, useful for large Cartesian space
390
399
  - `title`: title of the progress bar
400
+ - `order`: can be `:first` or `:last` to make the function calculate before or after all other functions
391
401
  - `block`: a function that receives each vector and returns a computed value
392
402
 
393
403
  Functions show up in `.output` like additional (virtual) dimensions.
@@ -34,6 +34,7 @@ class FlexCartesian
34
34
  @dimensions = dimensions
35
35
  @conditions = []
36
36
  @derived = {}
37
+ @order = { first: nil, last: nil }
37
38
  @function_results = {} # key: Struct instance.object_id => { fname => value }
38
39
  @function_hidden = Set.new
39
40
  import(path, format: format) if path
@@ -71,11 +72,11 @@ class FlexCartesian
71
72
  end
72
73
  end
73
74
 
74
- def func(command = :print, name = nil, hide: false, progress: false, title: "calculating functions", &block)
75
+ def func(command = :print, name = nil, hide: false, progress: false, title: "calculating functions", order: nil, &block)
75
76
  case command
76
77
  when :add
77
78
  raise ArgumentError, "Function name and block required for :add" unless name && block_given?
78
- add_function(name, &block)
79
+ add_function(name, order: order, &block)
79
80
  @function_hidden.delete(name.to_sym)
80
81
  @function_hidden << name.to_sym if hide
81
82
 
@@ -89,10 +90,17 @@ def func(command = :print, name = nil, hide: false, progress: false, title: "cal
89
90
  else
90
91
  @derived.each do |fname, fblock|
91
92
  source = fblock.source rescue '(source unavailable)'
92
-
93
93
  body = source.sub(/^.*?\s(?=(\{|\bdo\b))/, '').strip
94
-
95
- puts " #{fname.inspect.ljust(12)}| #{body}#{@function_hidden.include?(fname) ? ' [HIDDEN]' : ''}"
94
+ order = ""
95
+ if @order.value?(fname.to_sym)
96
+ case @order.key(fname.to_sym)
97
+ when :first
98
+ order = " [FIRST]"
99
+ when :last
100
+ order = " [LAST]"
101
+ end
102
+ end
103
+ puts " #{fname.inspect.ljust(12)}| #{body}#{@function_hidden.include?(fname) ? ' [HIDDEN]' : ''}#{order}"
96
104
  end
97
105
  end
98
106
 
@@ -124,18 +132,38 @@ def func(command = :print, name = nil, hide: false, progress: false, title: "cal
124
132
  end
125
133
  end
126
134
 
127
- def add_function(name, &block)
135
+ def add_function(name, order: nil , &block)
128
136
  raise ArgumentError, "Block required" unless block_given?
129
137
  if reserved_function_names.include?(name.to_sym)
130
138
  raise ArgumentError, "Function name '#{name}' has been already added"
131
139
  elsif reserved_struct_names.include?(name.to_sym)
132
140
  raise ArgumentError, "Name '#{name}' has been reserved for internal method, you can't use it for a function"
133
141
  end
134
- @derived[name.to_sym] = block
142
+ if order == :last
143
+ @derived[name.to_sym] = block # add to the tail of the hash
144
+ @order[:last] = name.to_sym
145
+ elsif order == :first
146
+ @derived = { name.to_sym => block }.merge(@derived) # add to the head of the hash
147
+ @order[:first] = name.to_sym
148
+ elsif order == nil
149
+ if @order[:last] != nil
150
+ last_name = @order[:last]
151
+ last_body = @derived[last_name]
152
+ @derived.delete(@order[:last]) # remove the tail of the hash
153
+ @derived[name.to_sym] = block # add new function to the tail of the hash
154
+ @derived[last_name] = last_body # restore :last function in the tail of the hash
155
+ else
156
+ @derived[name.to_sym] = block
157
+ end
158
+ else
159
+ raise ArgumentError, "unknown function order '#{order}'"
160
+ end
135
161
  end
136
162
 
137
163
  def remove_function(name)
138
164
  @derived.delete(name.to_sym)
165
+ @order[:last] = nil if @order[:last] == name.to_sym
166
+ @order[:first] = nil if @order[:first] == name.to_sym
139
167
  end
140
168
 
141
169
  def cartesian(dims = nil, lazy: false)
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: flex-cartesian
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.2.2
4
+ version: 1.3.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Yury Rassokhin
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2025-07-28 00:00:00.000000000 Z
11
+ date: 2025-08-19 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: colorize