How to resolve the algorithm Search a list of records step by step in the Factor programming language
How to resolve the algorithm Search a list of records step by step in the Factor programming language
Table of Contents
Problem Statement
Many programming languages provide convenient ways to look for a known value in a simple list of strings or numbers. But what if the elements of the list are themselves compound records/objects/data-structures, and the search condition is more complex than a simple equality test? Write a function/method/etc. that can find the first element in a given list matching a given condition. It should be as generic and reusable as possible. (Of course if your programming language already provides such a feature, you can use that instead of recreating it.) Then to demonstrate its functionality, create the data structure specified under #Data set, and perform on it the searches specified under #Test cases. The data structure to be used contains the names and populations (in millions) of the 10 largest metropolitan areas in Africa, and looks as follows when represented in JSON: However, you shouldn't parse it from JSON, but rather represent it natively in your programming language.
If any of that is impossible or unreasonable in your programming language, then feel free to deviate, as long as you explain your reasons in a comment above your solution. If your programming language supports higher-order programming, then the most elegant way to implement the requested functionality in a generic and reusable way, might be to write a function (maybe called "find_index" or similar), that takes two arguments: If this is not the approach which would be most natural or idiomatic in your language, explain why, and show what is.
Let's start with the solution:
Step by Step solution about How to resolve the algorithm Search a list of records step by step in the Factor programming language
Source code in the factor programming language
USING: accessors io kernel math prettyprint sequences ;
IN: rosetta-code.search-list
TUPLE: city name pop ;
CONSTANT: data {
T{ city f "Lagos" 21.0 }
T{ city f "Cairo" 15.2 }
T{ city f "Kinshasa-Brazzaville" 11.3 }
T{ city f "Greater Johannesburg" 7.55 }
T{ city f "Mogadishu" 5.85 }
T{ city f "Khartoum-Omdurman" 4.98 }
T{ city f "Dar Es Salaam" 4.7 }
T{ city f "Alexandria" 4.58 }
T{ city f "Abidjan" 4.4 }
T{ city f "Casablanca" 3.98 }
}
! Print the index of the first city named Dar Es Salaam.
data [ name>> "Dar Es Salaam" = ] find drop .
! Print the name of the first city with under 5 million people.
data [ pop>> 5 < ] find nip name>> print
! Print the population of the first city starting with 'A'.
data [ name>> first CHAR: A = ] find nip pop>> .
You may also check:How to resolve the algorithm RSA code step by step in the Julia programming language
You may also check:How to resolve the algorithm Catalan numbers step by step in the ERRE programming language
You may also check:How to resolve the algorithm Address of a variable step by step in the 68000 Assembly programming language
You may also check:How to resolve the algorithm Exponentiation with infix operators in (or operating on) the base step by step in the Julia programming language
You may also check:How to resolve the algorithm Update a configuration file step by step in the BASIC programming language