working on it ...

Filters

Explore Public Snippets

Sort by

Found 568 snippets matching: iteration

    public by lbottaro  332824  2  4  0

    C# enum iteration

    This c# code allows to iterate through all element of an enum
    public enum Suit
    {
            Spades,
            Hearts,
            Clubs,
            Diamonds
    }
    
    foreach (Suit suit in Enum.GetValues(typeof(Suit)))
    {
       Console.WriteLine(suit);
    }

    public by lbottaro  4546  2  8  5

    Functions as first class objects in Python

    Python allows the coder to treat functions as objects, also known as first class objects. Usually a first class object is an entity that can be dynamically created, destroyed, passed to a function, returned as a value, and have all the rights as other variables in the programming language have. In this case the functional programming paradigm al
    #!/usr/local/bin/python2.7
        
    def add(a, b):
      return a + b
    
    def subtract(a, b):
      return a - b
    
    def multiply(a, b):
      return a * b
    
    def divide(a, b):
      if b:
        return a /b
      else:
        return "Error, divide by zero!"
        
    def power(a, b):
      return a ** b
      
    def modulus(a, b):
      return a % b
    
    functions = [add, subtract, multiply, divide, power, modulus]
    
    for function in functions:
    
      name = function.__name__
      result = function(4.0,5.0)
      print(name + ": " + str(result))

    public by lbottaro  4152  8  7  0

    Bin bash array set and looping

    This linux script sets a variable as array, then loops through all its items.
    #!/bin/bash
    
    my_users_list=( elem1 elem2 elem3 )
    
    for user in ${my_users_list[@]}
    do
      echo $user
    done

    public by lbottaro  2142  1  6  0

    Tcsh array set and looping

    This linux script sets a variable as array, then makes a loop on all its items
    set all_users_x = ( name1 name2 )
    set all_users = ( $all_users_x )
    foreach user ($all_users)
       echo $user
    end
    
    

    public by lbottaro  2948  1  5  0

    linux bash while statement script

    This is a very simple example of while statement in linux bin bash console. The while condition terminates after N iterations, printing a counter for each loop.
    #!/bin/bash
    name=`basename $0`
    echo $name
    counter=0
    while [ $counter -lt 10 ]
    do
      echo "I'm live!"
      counter=`expr $counter + 1`
      echo "counter" $counter
      sleep 10
    done
    exit 0

    public by panshulgupta  2183  2  5  0

    Java Queue

    Using queues to iterate over a collection which can add elements during iteration of the collection
    Queue<Integer> queue = new LinkedList<Integer>();
    queue.add(1);
    queue.add(2);
    queue.add(3);
    queue.add(4);
    
    while (!queue.isEmpty()) {
        Integer i = queue.remove();
        if (i == 2)
            queue.add(42);
    
        System.out.println(i);
    }

    public by dursunkoc  1598  0  4  0

    iterating a Collection (List, Set etc)

    iterating a Collection (List, Set etc): collection iteration
    Collection<T> coll = ...
    Iterator<T> iter = coll.iterator();
    while (iter.hasNext()){
      T next = iter.next();
      ...
    }
    
    

    public by msdn  1047  0  7  0

    CreateCombinations: Allows for iteration through all combinations from an array.

    Allows for iteration through all combinations from an array. The size of the subset. The numbers. An enumerable for all the combinations.
    using System;
    using System.Collections.Generic;
    using Microsoft.ParallelComputingPlatform.ParallelExtensions.Samples.Sudoku.Collections;
    
    /// <summary>Allows for iteration through all combinations from an array.</summary>
    /// <param name="size">The size of the subset.</param>
    /// <param name="numbers">The numbers.</param>
    /// <returns>An enumerable for all the combinations.</returns>
    public static IEnumerable<int[]> CreateCombinations(int size, int[] numbers)
    {
        int[] indices = new int[size];
        int[] result = new int[size];
        for (int i = 0; i < indices.Length; i++)
        {
            indices[i] = i;
            result[i] = numbers[i];
        }
        yield return result;
    
        while (true)
        {
            if (indices[0] >= numbers.Length - size) yield break;
    
            // Find the next position to be incremented
            int pos;
            for (pos = indices.Length - 1;
                pos > 0 && indices[pos] == numbers.Length - size + pos;
                pos--) ;
    
            // Increment it
            indices[pos]++;
    
            // Increment everything else
            for (int j = pos; j < size - 1; j++) indices[j + 1] = indices[j] + 1;
    
            // Create the result
            for (int i = 0; i < indices.Length; i++) result[i] = numbers[indices[i]];
            yield return result;
        }
    }

    public by msdn  1446  0  7  0

    For: Executes a for loop in which iterations may run in parallel.

    Executes a for loop in which iterations may run in parallel. The start index, inclusive. The end index, exclusive. A System.Threading.Tasks.ParallelOptions instance that configures the behavior of this operation. The delegate that is invoked once per iteration.
    using System.Collections.Generic;
    using System.Numerics;
    using System.Threading.Tasks;
    
    /// <summary>Executes a for loop in which iterations may run in parallel.</summary>
    /// <param name="fromInclusive">The start index, inclusive.</param>
    /// <param name="toExclusive">The end index, exclusive.</param>
    /// <param name="options">A System.Threading.Tasks.ParallelOptions instance that configures the behavior of this operation.</param>
    /// <param name="body">The delegate that is invoked once per iteration.</param>
    public static void For(BigInteger fromInclusive, BigInteger toExclusive, ParallelOptions options, Action<BigInteger> body)
    {
        // Determine how many iterations to run...
        var range = toExclusive - fromInclusive;
    
        // ... and run them.
        if (range <= 0)
        {
            // If there's nothing to do, bail
            return;
        }
            // Fast path
        else if (range <= Int64.MaxValue)
        {
            // If the range is within the realm of Int64, we'll delegate to Parallel.For's Int64 overloads.
            // Iterate from 0 to range, and then call the user-provided body with the scaled-back value.
            Parallel.For(0, (long)range, options, i => body(i + fromInclusive));
        }
            // Slower path
        else
        {
            // For a range larger than Int64.MaxValue, we'll rely on an enumerable of BigInteger.
            // We create a C# iterator that yields all of the BigInteger values in the requested range
            // and then ForEach over that range.
            Parallel.ForEach(Range(fromInclusive, toExclusive), options, body);
        }
    }

    public by msdn  1092  0  7  0

    ForRange: Executes a for loop over ranges in which iterations may run in parallel.

    Executes a for loop over ranges in which iterations may run in parallel. The start index, inclusive. The end index, exclusive. The function delegate that returns the initial state of the local data for each thread. The delegate that is invoked once per range. The delegate that performs a final action on the local state of each thread. A Para
    using System.Collections.Concurrent;
    using System.Threading.Tasks;
    
    /// <summary>Executes a for loop over ranges in which iterations may run in parallel. </summary>
    /// <param name="fromInclusive">The start index, inclusive.</param>
    /// <param name="toExclusive">The end index, exclusive.</param>
    /// <param name="localInit">The function delegate that returns the initial state of the local data for each thread.</param>
    /// <param name="body">The delegate that is invoked once per range.</param>
    /// <param name="localFinally">The delegate that performs a final action on the local state of each thread.</param>
    /// <returns>A ParallelLoopResult structure that contains information on what portion of the loop completed.</returns>
    public static ParallelLoopResult ForRange<TLocal>(
        long fromInclusive, long toExclusive,
        ParallelOptions parallelOptions,
        Func<TLocal> localInit,
        Func<long, long, ParallelLoopState, TLocal, TLocal> body,
        Action<TLocal> localFinally)
    {
        if (parallelOptions == null) throw new ArgumentNullException("parallelOptions");
        if (localInit == null) throw new ArgumentNullException("localInit");
        if (body == null) throw new ArgumentNullException("body");
        if (localFinally == null) throw new ArgumentNullException("localFinally");
    
        return Parallel.ForEach(Partitioner.Create(fromInclusive, toExclusive), parallelOptions, localInit, (range, loopState, x) =>
        {
            return body(range.Item1, range.Item2, loopState, x);
        }, localFinally);
    }
    • Public Snippets
    • Channels Snippets