1. store bug

Wen I run following code I get a wrong result:

include std/sequence.e 
include std/pretty.e 
include std/console.e 
 
sequence s = {"first", {}} 
sequence t = store(s, {2}, {"second", {}}) 
 
pretty_print(1, t, {3}) 
puts(1, "\n") 
maybe_any_key() 

{ 
  "first", 
  { 
    "first", 
    { 
      "second", 
      "" 
    } 
  } 
} 

So I push back my proposal of April 8th 2018 to replace store and fetch by following code that works:

include std/search.e  
include std/text.e  
include std/pretty.e  
include std/console.e  
 
--------------------------------------------------------------------------------  
   
public function get_nested(object structure, sequence targetPath, object def=0,  
                           sequence currentPath={})   
  if equal(currentPath, targetPath) then  
    return structure  
  else  
    integer digit = targetPath[length(currentPath)+1]  
    if digit <= length(structure) then  
      return get_nested(structure[digit], targetPath, def, currentPath&digit)  
    else  
      return def  
    end if  
  end if  
end function   
   
--------------------------------------------------------------------------------  
   
public function set_nested(object structure, sequence targetPath,  
                           object x, sequence currentPath={})   
  integer digit = targetPath[length(currentPath)+1]  
  for i = 1 to digit do  
    if i > length(structure) then  
      structure = append(structure, {})  
    end if  
    if i = digit then   
      if equal(currentPath&{i}, targetPath) then  
        structure[i] = x  
      else  
        structure[i] = set_nested(structure[i], targetPath, x, currentPath&i)  
      end if  
    end if  
  end for  
  return structure   
end function  
  
--------------------------------------------------------------------------------  
  
public function remove_nested(object structure, sequence targetPath,  
                              sequence currentPath={})  
  integer digit = targetPath[length(currentPath)+1]  
  if digit <= length(structure) then  
    if equal(currentPath&{digit}, targetPath) then  
      structure = remove(structure, digit)  
    else  
      structure[digit] = remove_nested(structure[digit], targetPath,  
                                       currentPath&digit)  
    end if  
  end if  
  return structure  
end function  
  
------------------------------------------------------------------------------  
  
sequence s = {"first", {}} 
sequence t = set_nested(s, {2}, {"second", {}}) 
 
pretty_print(1, t, {3}) 
puts(1, "\n") 
maybe_any_key() 

{ 
  "first", 
  { 
    "second", 
    "" 
  } 
} 

Jean-Marc

new topic     » topic index » view message » categorize

2. Re: store bug

This should be a good replacement for store and fetch:

include std/text.e 
include std/search.e 
include std/error.e 
 
-------------------------------------------------------------------------------- 
 
function get_nested(object structure, sequence targetPath, object def=0, 
                           sequence currentPath={}) 
  if equal(currentPath, targetPath) then 
    return structure 
  else 
    integer digit = targetPath[length(currentPath)+1] 
    if digit <= length(structure) then 
      return get_nested(structure[digit], targetPath, def, currentPath&digit) 
    else 
      return def 
    end if 
  end if 
end function 
 
-------------------------------------------------------------------------------- 
 
function set_nested(object structure, sequence targetPath, 
                           object x, sequence currentPath={}) 
  integer digit = targetPath[length(currentPath)+1] 
  for i = 1 to digit do 
    if i > length(structure) then 
      structure = append(structure, {}) 
    end if 
    if i = digit then 
      if equal(currentPath&{i}, targetPath) then 
        structure[i] = x 
      else 
        structure[i] = set_nested(structure[i], targetPath, x, currentPath&i) 
      end if 
    end if 
  end for 
  return structure 
end function 
 
-------------------------------------------------------------------------------- 
 
public function store(object structure, sequence targetPath, object x) 
  if length(targetPath) = 0 then 
    crash_message("Target path cannot be empty!\n") 
  end if 
  return set_nested(structure, targetPath, x) 
end function 
 
-------------------------------------------------------------------------------- 
 
public function fetch(object structure, sequence targetPath, object def=0) 
  if length(targetPath) = 0 then 
    crash_message("Target path cannot be empty!\n") 
  end if 
  return get_nested(structure, targetPath, def) 
end function 
 
-------------------------------------------------------------------------------- 
 
public function remove_nested(object structure, sequence targetPath, 
                              sequence currentPath={}) 
  integer digit = targetPath[length(currentPath)+1] 
  if digit <= length(structure) then 
    if equal(currentPath&{digit}, targetPath) then 
      structure = remove(structure, digit) 
    else 
      structure[digit] = remove_nested(structure[digit], targetPath, 
                                       currentPath&digit) 
    end if 
  end if 
  return structure 
end function 
new topic     » goto parent     » topic index » view message » categorize

3. Re: store bug

This could be useful too:

function path_exists(object structure, sequence targetPath, 
                     sequence currentPath={}) 
  if equal(currentPath, targetPath) then 
    return 1 
  else 
    integer digit = targetPath[length(currentPath)+1] 
    if digit <= length(structure) then 
      return path_exists(structure[digit], targetPath, currentPath&digit) 
    else 
      return 0 
    end if 
  end if 
end function 
new topic     » goto parent     » topic index » view message » categorize

4. Re: store bug

jmduro said...

Wen I run following code I get a wrong result:

include std/sequence.e 
include std/pretty.e 
include std/console.e 
 
sequence s = {"first", {}} 
sequence t = store(s, {2}, {"second", {}}) 
 
pretty_print(1, t, {3}) 
puts(1, "\n") 
maybe_any_key() 

{ 
  "first", 
  { 
    "first", 
    { 
      "second", 
      "" 
    } 
  } 
} 

It looks like this has been corrected. When I run your code I get the expected result.

{ 
  "first", 
  { 
    "second", 
    "" 
  } 
} 

I used both Euphoria 4.1 and the current source build. I think this might be the commit that fixed it: 7ea6c0a.

Are you using 4.1? Or maybe using 4.0 includes with 4.1 binaries?

-Greg

new topic     » goto parent     » topic index » view message » categorize

5. Re: store bug

Thank you Greg,

I'm using

Euphoria Interpreter v4.1.0 development 
   32-bit Windows, Using System Memory 
   Revision Date: 2015-02-13 18:10:12, Id: 6318:1c30095180a0 

new topic     » goto parent     » topic index » view message » categorize

6. Re: store bug

jmduro said...

Thank you Greg,

I'm using

Euphoria Interpreter v4.1.0 development 
   32-bit Windows, Using System Memory 
   Revision Date: 2015-02-13 18:10:12, Id: 6318:1c30095180a0 

Open std/sequence.e and scroll to the start of store(). It should start on line 149 and include the following code to prevent this problem:

if length(indexes) = 1 then 
    target[indexes[1]] = x 
    return target 
end if 

If that is not the case then I suspect you're on the old 4.0 include files. (We need to work on marking include files with versions going forward to help prevent this.)

-Greg

new topic     » goto parent     » topic index » view message » categorize

7. Re: store bug

jmduro said...

Wen I run following code I get a wrong result:

-- [...] 
 
sequence s = {"first", {}} 
sequence t = store(s, {2}, {"second", {}}) 
 
pretty_print(1, t, {3}) 
puts(1, "\n") 
maybe_any_key() 

{ 
  "first", 
  { 
    "first", 
    { 
      "second", 
      "" 
    } 
  } 
} 
[..]

I've correct output. (current git-repo)

new topic     » goto parent     » topic index » view message » categorize

8. Re: store bug

Thank you Greg,

These lines were not in std/sequence.e. I have updated my EU version.

Jean-Marc

new topic     » goto parent     » topic index » view message » categorize

Search



Quick Links

User menu

Not signed in.

Misc Menu