Trusted answers to developer questions
Trusted Answers to Developer Questions

Related Tags

euphoria
communitycreator

What is the fetch() method in Euphoria?

NDUKWE CHIDERA K.

Grokking Modern System Design Interview for Engineers & Managers

Ace your System Design Interview and take your career to the next level. Learn to handle the design of applications like Netflix, Quora, Facebook, Uber, and many more in a 45-min interview. Learn the RESHADED framework for architecting web-scale applications by determining requirements, constraints, and assumptions before diving into a step-by-step design process.

Answers Code

Overview

In Euphoria, the fetch method is a built-in method that retrieves an element from a sequence no matter how deeply nested it is in the sequence.

Suppose we have a sequence A = {2,3,{7,8,9 {"abc","xyz"}},12}. We want to fetch element y from A. The following steps show how deep the algorithm goes into A to achieve this.

  • The first layer contains A, which is a sequence with four elements 2 and 3 and {7,8,9,{"abc","xyz"}} and lastly 12.

  • The second layer is the third element of A, which has four sub-elements: 7 and 8 and 9 and {"abc","xyz"}.

  • The third layer is the subsequence: {"abc","xyz"}. Here, we can see that y is in the second element of this subsequence.

  • The fourth layer is the string subsequence: “xyz”. We can fetch the element y from the second element of this subsequence.

That is how deep the algorithm shall run to get y. For the fetch() method to go this deep, we have to provide the path using indexes of the different elements involved in the path. In the example above, the indexes required to fetch y from A would-be {3,4,2,2}.

Syntax

fetch(source,path)

Parameters

  • source: This sequence may have our fetch nested into it.

  • path: This sequence contains a list of indexes from source that will serve as the path to the fetch element.

Note: If the path provided can not lead to getting an element in the source, a nonexistent element error will occur.

Example

include std/sequence.e
sequence source1, source2
sequence result1, result2
source1 = {0,1,2,{5,6,{10,11,30,{1,"you"}}}}
source2 = {2,3,{7,8,9,{"abc","xyz"}},12}
printf(1, "for source1 : %s", fetch(source1 ,{4,3,4,2,3}))
printf(1,"\nfor source2 : %s", fetch(source2,{3,4,2,2}))

Explanation

  • Line 1: We include the sequence.e module to use the fetch method.

  • Line 3–6: We declare and initialize the necessary parameters.

  • Line 9–10: We call the fetch() method and print the result of its operation.

RELATED TAGS

euphoria
communitycreator

Grokking Modern System Design Interview for Engineers & Managers

Ace your System Design Interview and take your career to the next level. Learn to handle the design of applications like Netflix, Quora, Facebook, Uber, and many more in a 45-min interview. Learn the RESHADED framework for architecting web-scale applications by determining requirements, constraints, and assumptions before diving into a step-by-step design process.

Answers Code
Keep Exploring