How to resolve the algorithm Search a list of records step by step in the EchoLisp programming language
How to resolve the algorithm Search a list of records step by step in the EchoLisp 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 EchoLisp programming language
Source code in the echolisp programming language
(require 'struct)
(require 'json)
;; importing data
(define cities
#<<
[{"name":"Lagos", "population":21}, {"name":"Cairo", "population":15.2}, {"name":"Kinshasa-Brazzaville", "population":11.3}, {"name":"Greater Johannesburg", "population":7.55}, {"name":"Mogadishu", "population":5.85}, {"name":"Khartoum-Omdurman", "population":4.98}, {"name":"Dar Es Salaam", "population":4.7}, {"name":"Alexandria", "population":4.58}, {"name":"Abidjan", "population":4.4}, {"name":"Casablanca", "population":3.98}]
>>#)
;; define a structure matching data
;; heterogenous slots values
(struct city (name population))
;; convert JSON to EchoLisp instances of structures
(set! cities
(vector-map (lambda(x) (json->struct x struct:city)) (json-import cities)))
;; search by name, case indifferent
(define (city-index name)
(vector-search (lambda(x) (string-ci=? (city-name x) name)) cities))
;; returns first city name such as population < seuil
(define (city-pop seuil)
(define idx (vector-search (lambda(x) (< (city-population x) seuil)) cities))
(if idx
(city-name (vector-ref cities idx))
(cons seuil 'not-found)))
(city-index "Dar Es Salaam") → 6
(city-pop 5) → "Khartoum-Omdurman"
(city-pop -666) → (-666 . not-found)
(city-index "alexandra") → #f
You may also check:How to resolve the algorithm Emirp primes step by step in the Factor programming language
You may also check:How to resolve the algorithm Metronome step by step in the Java programming language
You may also check:How to resolve the algorithm Color wheel step by step in the Kotlin programming language
You may also check:How to resolve the algorithm Y combinator step by step in the Quackery programming language
You may also check:How to resolve the algorithm Vigenère cipher step by step in the REXX programming language