| Did you know ... | Search Documentation: |
| Pack starlog -- VARIABLE_METHOD_CHAIN.md |
Implement all combinations and configurations of: ?- B is [3,2,2], A is B>>sort>>length.
Note: The problem statement notation B is [3,2,2] should be interpreted as variable assignment. In Prolog/Starlog, this is written as B = [3,2,2] (unification), since [3,2,2] is a plain list, not a Starlog expression. The is operator is reserved for Starlog expressions like `B is [1,2]&[3,4] or B is reverse([3,2,1])`.
This implementation demonstrates comprehensive support for using variables in method chains. The pattern allows:
= or Starlog expressions with is)>> operatorB = [3,2,2], A is B>>sort>>length showcases how variables can hold values and then be used in fluent method chains.?- B = [3,2,2], starlog_call(A is B>>sort>>length). B = [3,2,2], A = 2.
B = [3,2,2] assigns the list to variable BB>>sort>>length pipes B through sort, then through length?- B = [3,2,2], starlog_call(A is B>>sort). A = [2,3]. ?- B = [1,2,3], starlog_call(A is B>>reverse). A = [3,2,1]. ?- B = [1,2,3], starlog_call(A is B>>length). A = 3.
?- B = [5,1,3,2], starlog_call(A is B>>sort>>length). A = 4. ?- B = [[1],[2],[3]], starlog_call(A is B>>flatten>>reverse). A = [3,2,1]. ?- B = [3,1,2], starlog_call(A is B>>sort>>reverse>>length). A = 3.
?- B = [5,2,8,1], starlog_call(A is B>>max_list). A = 8. ?- B = [5,2,8,1], starlog_call(A is B>>min_list). A = 1. ?- B = [1,2,3,4], starlog_call(A is B>>sum_list). A = 10.
?- B = "hello", starlog_call(A is B>>string_upper). A = "HELLO". ?- B = "WORLD", starlog_call(A is B>>string_lower). A = "world".
?- B = "hello", starlog_call(A is B>>string_length). A = 5. ?- B = "test", starlog_call(A is B>>string_chars). A = [t,e,s,t].
?- B = "hello", starlog_call(A is B>>string_upper>>string_length). A = 5. ?- B = "test", starlog_call(A is B>>string_chars>>length). A = 4. ?- B = "hello", starlog_call(A is B>>string_upper>>string_chars>>length). A = 5.
?- B = hello, starlog_call(A is B>>atom_length). A = 5. ?- B = world, starlog_call(A is B>>atom_chars). A = [w,o,r,l,d]. ?- B = abc, starlog_call(A is B>>atom_chars>>length). A = 3.
?- B = -42, starlog_call(A is B>>abs). A = 42. ?- B = 3.7, starlog_call(A is B>>ceiling). A = 4. ?- B = 3.2, starlog_call(A is B>>floor). A = 3. ?- B = 3.5, starlog_call(A is B>>round). A = 4. ?- B = 16, starlog_call(A is B>>sqrt). A = 4.0.
?- starlog_call(B is [1,2]&[3,4]), starlog_call(A is B>>reverse). B = [1,2,3,4], A = [4,3,2,1]. ?- starlog_call(B is [1,2]&[3,4]), starlog_call(A is B>>sort>>length). B = [1,2,3,4], A = 4.
?- starlog_call(B is "hello":"world"), starlog_call(A is B>>string_length). B = "helloworld", A = 10. ?- starlog_call(B is "hello":"world"), starlog_call(A is B>>string_upper). B = "helloworld", A = "HELLOWORLD".
?- starlog_call(B is reverse([1,2,3])), starlog_call(A is B>>length). B = [3,2,1], A = 3. ?- starlog_call(B is sort([3,1,2])), starlog_call(A is B>>reverse). B = [1,2,3], A = [3,2,1].
?- B = [3,2,2], starlog_call(C is B>>sort), starlog_call(A is C>>length). B = [3,2,2], C = [2,3], A = 2.
?- B = "hello", starlog_call(C is B>>string_upper), starlog_call(A is C>>string_length). B = "hello", C = "HELLO", A = 5.
?- B = [1,2,3], starlog_call(C is B>>reverse), starlog_call(D is C>>sort), starlog_call(A is D>>length). B = [1,2,3], C = [3,2,1], D = [1,2,3], A = 3.
?- B = [], starlog_call(A is B>>length). A = 0. ?- B = "", starlog_call(A is B>>string_length). A = 0.
?- B = [5], starlog_call(A is B>>sort>>length). A = 1.
?- B = [1,2,3], starlog_call(A is B>>sort). A = [1,2,3]. % Already sorted ?- B = [1,1,1], starlog_call(A is B>>sort>>length). A = 1. % Note: Prolog's sort/2 removes duplicates
Note: Prolog's sort/2 predicate removes duplicate elements while sorting. This is standard Prolog behavior. For sorting without removing duplicates, use msort/2 instead.
This pattern demonstrates the seamless integration of:
B = [3,2,2]B>>sortB>>sort>>lengthB>>sort>>reverse>>length[1,2,3]"hello"hello42, -42, 3.7reverse, sort, flatten, length, max_list, min_list, sum_liststring_length, string_upper, string_lower, string_charsatom_length, atom_charsabs, ceiling, floor, round, sqrttests/test_variable_method_chain.plComprehensive test suite with 42 tests covering:
demo_variable_method_chain.plInteractive demonstration showing:
VARIABLE_METHOD_CHAIN.mdThis documentation file explaining:
:- use_module(starlog).
process_data(Input, Result) :-
% Assign input to variable
Data = Input,
% Apply transformations via method chain
starlog_call(Result is Data>>sort>>reverse>>length).
?- process_data([3,2,2], R).
R = 2.
?- use_module(starlog). ?- B = [3,2,2], starlog_call(A is B>>sort>>length). B = [3,2,2], A = 2.
?- B = [3,2,2], starlog_call(C is B>>sort), starlog_call(A is C>>length). B = [3,2,2], C = [2,3], A = 2.
This feature is fully compatible with:
:, &, •)Run the test suite:
swipl -s tests/test_variable_method_chain.pl
Run the demonstration:
swipl -s demo_variable_method_chain.pl
The pattern B = [3,2,2], A is B>>sort>>length and all its combinations and configurations are fully supported and demonstrated. The implementation covers:
✓ All data types (lists, strings, atoms, numbers) ✓ All assignment patterns (=, is with Starlog expressions) ✓ All method chain lengths (1, 2, 3+ operations) ✓ All operation types (transformation, analysis, aggregation) ✓ Multiple variables in sequence ✓ Edge cases (empty, single element, pre-processed) ✓ 40+ working examples ✓ Comprehensive test coverage ✓ Full documentation
The feature is complete and ready for use.