traversal

Can someone explain the traverse function in Haskell?

我的梦境 提交于 2019-11-29 19:07:26
I am trying and failing to grok the traverse function from Data.Traversable . I am unable to see its point. Since I come from an imperative background, can someone please explain it to me in terms of an imperative loop? Pseudo-code would be much appreciated. Thanks. traverse is the same as fmap , except that it also allows you to run effects while you're rebuilding the data structure. Take a look at the example from the Data.Traversable documentation. data Tree a = Empty | Leaf a | Node (Tree a) a (Tree a) The Functor instance of Tree would be: instance Functor Tree where fmap f Empty = Empty

jQuery find parent form

丶灬走出姿态 提交于 2019-11-29 18:57:59
i have this html <ul> <li><form action="#" name="formName"></li> <li><input type="text" name="someName" /></li> <li><input type="text" name="someOtherName" /></li> <li><input type="submit" name="submitButton" value="send"></li> <li></form></li> </ul> How can i select the form that the input[name="submitButton"] is part of ? (when i click on the submit button i want to select the form and append some fields in it) I would suggest using closest , which selects the closest matching parent element: $('input[name="submitButton"]').closest("form"); Instead of filtering by the name, I would do this:

Dynamic Array traversal in PHP

為{幸葍}努か 提交于 2019-11-29 15:16:54
I want to build a hierarchy from a one-dimensional array and can (almost) do so with a more or less hardcoded code. How can I make the code dynamic? Perhaps with while(isset($array[$key])) { ... } ? Or, with an extra function? Like this: $out = my_extra_traverse_function($array,$key); function array_traverse($array,$key=NULL) { $out = (string) $key; $out = $array[$key] . "/" . $out; $key = $array[$key]; $out = $array[$key] ? $array[$key] . "/" . $out : ""; $key = $array[$key]; $out = $array[$key] ? $array[$key] . "/" . $out : ""; $key = $array[$key]; $out = $array[$key] ? $array[$key] . "/" .

get the next element with a specific class after a specific element

核能气质少年 提交于 2019-11-29 13:57:09
I have a HTML markup like this: <p> <label>Arrive</label> <input id="from-date1" class="from-date calender" type="text" /> </p> <p> <label>Depart</label> <input id="to-date1" class="to-date calender" type="text" /> </p> <p> <label>Arrive</label> <input id="from-date2" class="from-date calender" type="text" /> </p> <p> <label>Depart</label> <input id="to-date2" class="to-date calender" type="text" /> </p> I want to get the next element after from dates to get the corresponding to date. (Layout is a little more complex but from date has from-date class and to date has to-date class). This is I

How to sequence Either with Scala cats without a type alias (see Herding cats)

痞子三分冷 提交于 2019-11-29 12:24:20
I was reading Herding Cats The final example on the Traverse page on sequencing List of Either failed for me. in the example they do this:- scala> List(Right(1): Either[String, Int]).sequence res5: Either[String,List[Int]] = Right(List(1)) scala> List(Right(1): Either[String, Int], Left("boom"): Either[String, Int]).sequence res6: Either[String,List[Int]] = Left(boom) But When I try I get the following error:- scala> import cats._, cats.data._, cats.implicits._ scala> val les = List(Right(3):Either[String,Int], Right(2):Either[String,Int]) scala> les.sequence <console>:37: error: Cannot prove

Traversing FTP listing

非 Y 不嫁゛ 提交于 2019-11-29 11:38:35
I am trying to to get all directories' name from an FTP server and store them in hierarchical order in a multidimensional list or dict So for example, a server that contains the following structure: /www/ mysite.com images png jpg at the end of the script, would give me a list such as ['/www/' ['mysite.com' ['images' ['png'], ['jpg'] ] ] ] I have tried using a recursive function like so: def traverse(dir): FTP.dir(dir, traverse) FTP.dir returns lines in this format: drwxr-xr-x 5 leavesc1 leavesc1 4096 Nov 29 20:52 mysite.com so doing line[56:] will give me just the directory name(mysite.com).

DOM: fetch all text nodes in the document (PHP)

一个人想着一个人 提交于 2019-11-29 11:22:29
I've have the following (PHP) code that traverses an entire DOM document to get all of the text nodes. It's a bit of a ugly solution, and I'm sure there must be a better way... so, is there? $skip = false; $node = $document; $nodes = array(); while ($node) { if ($node->nodeType == 3) { $nodes[] = $node; } if (!$skip && $node->firstChild) { $node = $node->firstChild; } elseif ($node->nextSibling) { $node = $node->nextSibling; $skip = false; } else { $node = $node->parentNode; $skip = true; } } Thanks. The XPath expression you need is //text() . Try using it with DOMXPath::query . For example:

Check if an array is sorted, return true or false

依然范特西╮ 提交于 2019-11-29 09:11:52
问题 I am writing an easy program the just returns true if an array is sorted else false and I keep getting an exception in eclipse and I just can't figure out why. I was wondering if someone could take a look at my code and kind of explain why I'm getting an array out of bounds exception. public static boolean isSorted(int[] a) { int i; for(i = 0; i < a.length; i ++);{ if (a[i] < a[i+1]) { return true; } else { return false; } } } public static void main(String[] args) { int ar[] = {3,5,6,7};

Traverse an array diagonally

淺唱寂寞╮ 提交于 2019-11-29 09:09:44
问题 I have a large array of arbitrary size. It's a square array. I'm trying to grasp how to traverse it diagonally like a / rather than a \ (what I already know how to do). I have the following code thus far: char[][] array = new char[500][500]; //array full of random letters String arrayLine = ""; for (int y = 0; y < array.length; y++) { for (int x = 0; x < array.length; x++) { for (???) { arrayLine = arrayLine + array[???][???]; } } System.out.println(arrayLine); } I have three loops, because

jQuery: Getting the two last list items?

痴心易碎 提交于 2019-11-29 09:09:14
I want to apply a special class to the two last list items in an unordered list with jQuery. Like this: <ul> <li>Lorem</li> <li>ipsum</li> <li>dolor</li> <li class="special">sit</li> <li class="special">amet</li> </ul> How to? Should I use :eq somehow? Thanks in advance Pontus Another approach, using andSelf and prev : $('ul li:last-child').prev('li').andSelf().addClass("special"); vkostromin You can use function slice . It is very flexible. $('ul li').slice(-2).addClass("special"); var items = $('ul li') var last_two = items.filter('li:gt('+ items.length-3 +')') last_two.addClass('special');