text:
{-# LANGUAGE MultiParamTypeClasses #-}
{-# LANGUAGE FlexibleInstances #-}
{-# LANGUAGE TypeFamilies #-}

-- | This module contains a collection of generalized graph search algorithms,
-- for when you don't want to explicitly represent your data as a graph. The
-- general idea is to provide these algorithms with a way of generating "next"
-- states, a way of generating associated information, a way of determining
-- when you have found a solution, and an initial state.
module Algorithm.Search (
  -- * Searches
  bfs,
  dfs,
  dijkstra,
  dijkstraAssoc,
  aStar,
  aStarAssoc,
  -- * Monadic Searches
  -- $monadic
  bfsM,
  dfsM,
  dijkstraM,
  dijkstraAssocM,
  aStarM,
  aStarAssocM,
  -- * Utility
  incrementalCosts,
  incrementalCostsM,
  pruning,
  pruningM
  ) where

import qualified Data.Map as Map
import qualified Data.Sequence as Seq
import qualified Data.Set as Set
import qualified Data.List as List
import qualified Data.Foldable as Foldable
import Data.Functor.Identity (Identity(..))
import Control.Monad (filterM, zipWithM)

-- | @bfs next found initial@ performs a breadth-first search over a set of
-- states, starting with @initial@, and generating neighboring states with
-- @next@. It returns a path to a state for which @found@ returns 'True'.
-- Returns 'Nothing' if no path is possible.
--
-- === Example: Making change problem
--
-- >>> :{
-- countChange target = bfs (add_one_coin `pruning` (> target)) (== target) 0
--   where
--     add_one_coin amt = map (+ amt) coins
--     coins = [25, 10, 5, 1]
-- :}
--
-- >>> countChange 67
-- Just [25,50,60,65,66,67]
bfs :: (Foldable f, Ord state)
  => (state -> f state)
  -- ^ Function to generate "next" states given a current state
  -> (state -> Bool)
  -- ^ Predicate to determine if solution found. 'bfs' returns a path to the
  -- first state for which this predicate returns 'True'.
  -> state
  -- ^ Initial state
  -> Maybe [state]
  -- ^ First path found to a state matching the predicate, or 'Nothing' if no
  -- such path exists.
bfs =
  -- BFS is a generalized search using a queue, which directly compares states,
  -- and which always uses the first path found to a state
  generalizedSearch Seq.empty id (\_ _ -> False)

-- | @dfs next found initial@ performs a depth-first search over a set
-- of states, starting with @initial@ and generating neighboring states with
-- @next@. It returns a depth-first path to a state for which @found@ returns
-- 'True'. Returns 'Nothing' if no path is possible.
--
-- === Example: Simple directed graph search
--
-- >>> import qualified Data.Map as Map
--
-- >>> graph = Map.fromList [(1, [2, 3]), (2, [4]), (3, [4]), (4, [])]
--
-- >>> dfs (graph Map.!) (== 4) 1
-- Just [3,4]
dfs :: (Foldable f, Ord state)
  => (state -> f state)
  -- ^ Function to generate "next" states given a current state. These should be
  -- given in the order in which states should be pushed onto the stack, i.e.
  -- the "last" state in the Foldable will be the first one visited.
  -> (state -> Bool)
  -- ^ Predicate to determine if solution found. 'dfs' returns a path to the
  -- first state for which this predicate returns 'True'.
  -> state
  -- ^ Initial state
  -> Maybe [state]
  -- ^ First path found to a state matching the predicate, or 'Nothing' if no
  -- such path exists.
dfs =
  -- DFS is a generalized search using a stack, which directly compares states,
  -- and which always uses the most recent path found to a state
  generalizedSearch [] id (\_ _ -> True)

-- | @dijkstra next cost found initial@ performs a shortest-path search over
-- a set of states using Dijkstra's algorithm, starting with @initial@,
-- generating neighboring states with @next@, and their incremental costs with
-- @costs@. This will find the least-costly path from an initial state to a
-- state for which @found@ returns 'True'. Returns 'Nothing' if no path to a
-- solved state is possible.
--
-- === Example: Making change problem, with a twist
--
-- >>> :{
-- -- Twist: dimes have a face value of 10 cents, but are actually rare
-- -- misprints which are worth 10 dollars
-- countChange target =
--   dijkstra (add_coin `pruning` (> target)) true_cost  (== target) 0
--   where
--     coin_values = [(25, 25), (10, 1000), (5, 5), (1, 1)]
--     add_coin amt = map ((+ amt) . snd) coin_values
--     true_cost low high =
--       case lookup (high - low) coin_values of
--         Just val -> val
--         Nothing -> error $ "invalid costs: " ++ show high ++ ", " ++ show low
-- :}
--
-- >>> countChange 67
-- Just (67,[1,2,7,12,17,42,67])
dijkstra :: (Foldable f, Num cost, Ord cost, Ord state)
  => (state -> f state)
  -- ^ Function to generate list of neighboring states given the current state
  -> (state -> state -> cost)
  -- ^ Function to generate transition costs between neighboring states. This is
  -- only called for adjacent states, so it is safe to have this function be
  -- partial for non-neighboring states.
  -> (state -> Bool)
  -- ^ Predicate to determine if solution found. 'dijkstra' returns the shortest
  -- path to the first state for which this predicate returns 'True'.
  -> state
  -- ^ Initial state
  -> Maybe (cost, [state])
  -- ^ (Total cost, list of steps) for the first path found which
  -- satisfies the given predicate
dijkstra next cost found initial =
  -- This API to Dijkstra's algorithm is useful when the state transition
  -- function and the cost function are logically separate.
  -- It is implemented by using @dijkstraAssoc@ with appropriate mapping of
  -- arguments.
  dijkstraAssoc next' found initial
  where
    next' st = map (\new_st -> (new_st, cost st new_st)) $
               Foldable.toList (next st)

-- | @dijkstraAssoc next found initial@ performs a shortest-path search over
-- a set of states using Dijkstra's algorithm, starting with @initial@,
-- generating neighboring states with associated incremenal costs with
-- @next@. This will find the least-costly path from an initial state to a
-- state for which @found@ returns 'True'. Returns 'Nothing' if no path to a
-- solved state is possible.
dijkstraAssoc :: (Num cost, Ord cost, Ord state)
  => (state -> [(state, cost)])
  -- ^ function to generate list of neighboring states with associated
  -- transition costs given the current state
  -> (state -> Bool)
  -- ^ Predicate to determine if solution found. 'dijkstraAssoc' returns the
  -- shortest path to the first state for which this predicate returns 'True'.
  -> state
  -- ^ Initial state
  -> Maybe (cost, [state])
  -- ^ (Total cost, list of steps) for the first path found which
  -- satisfies the given predicate
dijkstraAssoc next found initial =
  -- This API to Dijkstra's algoritm is useful in the common case when next
  -- states and their associated transition costs are generated together.
  --
  -- Dijkstra's algorithm can be viewed as a generalized search, with the search
  -- container being a heap, with the states being compared without regard to
  -- cost, with the shorter paths taking precedence over longer ones, and with
  -- the stored state being (cost so far, state).
  -- This implementation makes that transformation, then transforms that result
  -- back into the desired result from @dijkstraAssoc@
  unpack <$>
    generalizedSearch emptyLIFOHeap snd leastCostly next' (found . snd)
      (0, initial)
  where
    next' (old_cost, st) =
      (\(new_st, new_cost) -> (new_cost + old_cost, new_st))
        <$> (next st)
    unpack [] = (0, [])
    unpack packed_states = (fst . last $ packed_states, map snd packed_states)

-- | @aStar next cost remaining found initial@ performs a best-first search
-- using the A* search algorithm, starting with the state @initial@, generating
-- neighboring states with @next@, their cost with @cost@, and an estimate of
-- the remaining cost with @remaining@. This returns a path to a state for which
-- @found@ returns 'True'. If @remaining@ is strictly a lower bound on the
-- remaining cost to reach a solved state, then the returned path is the
-- shortest path. Returns 'Nothing' if no path to a solved state is possible.
--
-- === Example: Path finding in taxicab geometry
--
-- >>> :{
-- neighbors (x, y) = [(x, y + 1), (x - 1, y), (x + 1, y), (x, y - 1)]
-- dist (x1, y1) (x2, y2) = abs (y2 - y1) + abs (x2 - x1)
-- start = (0, 0)
-- end = (0, 2)
-- isWall = (== (0, 1))
-- :}
--
-- >>> aStar (neighbors `pruning` isWall) dist (dist end) (== end) start
-- Just (4,[(1,0),(1,1),(1,2),(0,2)])
aStar :: (Foldable f, Num cost, Ord cost, Ord state)
  => (state -> f state)
  -- ^ Function to generate list of neighboring states given the current state
  -> (state -> state -> cost)
  -- ^ Function to generate transition costs between neighboring states. This is
  -- only called for adjacent states, so it is safe to have this function be
  -- partial for non-neighboring states.
  -> (state -> cost)
  -- ^ Estimate on remaining cost given a state
  -> (state -> Bool)
  -- ^ Predicate to determine if solution found. 'aStar' returns the shortest
  -- path to the first state for which this predicate returns 'True'.
  -> state
  -- ^ Initial state
  -> Maybe (cost, [state])
  -- ^ (Total cost, list of steps) for the first path found which satisfies the
  -- given predicate
aStar next cost remaining found initial =
  -- This API to A* search is useful when the state transition
  -- function and the cost function are logically separate.
  -- It is implemented by using @aStarAssoc@ with appropriate mapping of
  -- arguments.
  aStarAssoc next' remaining found initial
  where
    next' st = map (\new_st -> (new_st, cost st new_st)) $
               Foldable.toList (next st)

-- | @aStarAssoc next remaining found initial@ performs a best-first search
-- using the A* search algorithm, starting with the state @initial@, generating
-- neighboring states and their associated costs with @next@, and an estimate of
-- the remaining cost with @remaining@. This returns a path to a state for which
-- @found@ returns 'True'. If @remaining@ is strictly a lower bound on the
-- remaining cost to reach a solved state, then the returned path is the
-- shortest path. Returns 'Nothing' if no path to a solved state is possible.
aStarAssoc :: (Num cost, Ord cost, Ord state)
  => (state -> [(state, cost)])
  -- ^ function to generate list of neighboring states with associated
  -- transition costs given the current state
  -> (state -> cost)
  -- ^ Estimate on remaining cost given a state
  -> (state -> Bool)
  -- ^ Predicate to determine if solution found. 'aStar' returns the shortest
  -- path to the first state for which this predicate returns 'True'.
  -> state
  -- ^ Initial state
  -> Maybe (cost, [state])
  -- ^ (Total cost, list of steps) for the first path found which satisfies the
  -- given predicate
aStarAssoc next remaining found initial =
  -- This API to A* search is useful in the common case when next
  -- states and their associated transition costs are generated together.
  --
  -- A* can be viewed as a generalized search, with the search container being a
  -- heap, with the states being compared without regard to cost, with the
  -- shorter paths taking precedence over longer ones, and with
  -- the stored state being (total cost estimate, (cost so far, state)).
  -- This implementation makes that transformation, then transforms that result
  -- back into the desired result from @aStarAssoc@
  unpack <$> generalizedSearch emptyLIFOHeap snd2 leastCostly next'
    (found . snd2) (remaining initial, (0, initial))
  where
    next' (_, (old_cost, old_st)) =
      update_state <$> (next old_st)
      where
        update_state (new_st, cost) =
          let new_cost = old_cost + cost
              new_est = new_cost + remaining new_st
          in (new_est, (new_cost, new_st))
    unpack [] = (0, [])
    unpack packed_states =
      (fst . snd . last $ packed_states, map snd2 packed_states)
    snd2 = snd . snd

-- $monadic
-- Note that for all monadic searches, it is up to the user to ensure that
-- side-effecting monads do not logically change the structure of the graph.
-- For example, if the list of neighbors is being read from a file, the user
-- must ensure that those values do not change between reads.

-- | @bfsM@ is a monadic version of 'bfs': it has support for monadic @next@ and
-- @found@ parameters.
bfsM :: (Monad m, Foldable f, Ord state)
  => (state -> m (f state))
  -- ^ Function to generate "next" states given a current state
  -> (state -> m Bool)
  -- ^ Predicate to determine if solution found. 'bfsM' returns a path to the
  -- first state for which this predicate returns 'True'.
  -> state
  -- ^ Initial state
  -> m (Maybe [state])
  -- ^ First path found to a state matching the predicate, or 'Nothing' if no
  -- such path exists.
bfsM = generalizedSearchM Seq.empty id (\_ _ -> False)

-- | @dfsM@ is a monadic version of 'dfs': it has support for monadic @next@ and
-- @found@ parameters.
dfsM :: (Monad m, Foldable f, Ord state)
  => (state -> m (f state))
  -- ^ Function to generate "next" states given a current state
  -> (state -> m Bool)
  -- ^ Predicate to determine if solution found. 'dfsM' returns a path to the
  -- first state for which this predicate returns 'True'.
  -> state
  -- ^ Initial state
  -> m (Maybe [state])
  -- ^ First path found to a state matching the predicate, or 'Nothing' if no
  -- such path exists.
dfsM =
  generalizedSearchM [] id (\_ _ -> True)

-- | @dijkstraM@ is a monadic version of 'dijkstra': it has support for monadic
-- @next@, @cost@, and @found@ parameters.
dijkstraM :: (Monad m, Foldable f, Num cost, Ord cost, Ord state)
  => (state -> m (f state))
  -- ^ Function to generate list of neighboring states given the current state
  -> (state -> state -> m cost)
  -- ^ Function to generate list of costs between neighboring states. This is
  -- only called for adjacent states, so it is safe to have this function be
  -- partial for non-neighboring states.
  -> (state -> m Bool)
  -- ^ Predicate to determine if solution found. 'dijkstraM' returns the
  -- shortest path to the first state for which this predicate returns 'True'.
  -> state
  -- ^ Initial state
  -> m (Maybe (cost, [state]))
  -- ^ (Total cost, list of steps) for the first path found which
  -- satisfies the given predicate
dijkstraM nextM costM foundM initial =
  fmap2 unpack $ generalizedSearchM emptyLIFOHeap snd leastCostly nextM'
    (foundM . snd) (0, initial)
  where
    nextM' (old_cost, old_st) = do
      new_states <- Foldable.toList <$> nextM old_st
      incr_costs <- sequence $ costM old_st <$> new_states
      let new_costs = (+ old_cost) <$> incr_costs
      return $ zip new_costs new_states
    unpack [] = (0, [])
    unpack packed_states = (fst . last $ packed_states, map snd packed_states)

-- | @dijkstraAssocM@ is a monadic version of 'dijkstraAssoc': it has support
-- for monadic @next@ and @found@ parameters.
dijkstraAssocM :: (Monad m, Num cost, Ord cost, Ord state)
  => (state -> m [(state, cost)])
  -- ^ Function to generate list of neighboring states with associated
  -- transition costs given the current state
  -> (state -> m Bool)
  -- ^ Predicate to determine if solution found. 'dijkstraM' returns the
  -- shortest path to the first state for which this predicate returns 'True'.
  -> state
  -- ^ Initial state
  -> m (Maybe (cost, [state]))
  -- ^ (Total cost, list of steps) for the first path found which
  -- satisfies the given predicate
dijkstraAssocM nextM foundM initial =
  fmap2 unpack $ generalizedSearchM emptyLIFOHeap snd leastCostly nextM'
    (foundM . snd) (0, initial)
  where
    nextM' (old_cost, old_st) = do
      new_states <- nextM old_st
      return $ map (\(x, y) -> (y, x)) new_states
    unpack [] = (0, [])
    unpack packed_states = (fst . last $ packed_states, map snd packed_states)

-- | @aStarM@ is a monadic version of 'aStar': it has support for monadic
-- @next@, @cost@, @remaining@, and @found@ parameters.
aStarM :: (Monad m, Foldable f, Num cost, Ord cost, Ord state)
  => (state -> m (f state))
  -- ^ function to generate list of neighboring states with associated
  -- transition costs given the current state
  -> (state -> state -> m cost)
  -- ^ Function to generate list of costs between neighboring states. This is
  -- only called for adjacent states, so it is safe to have this function be
  -- partial for non-neighboring states.
  -> (state -> m cost)
  -- ^ Estimate on remaining cost given a state
  -> (state -> m Bool)
  -- ^ Predicate to determine if solution found. 'aStarM' returns the shortest
  -- path to the first state for which this predicate returns 'True'.
  -> state
  -- ^ Initial state
  -> m (Maybe (cost, [state]))
  -- ^ (Total cost, list of steps) for the first path found which satisfies the
  -- given predicate
aStarM nextM costM remainingM foundM initial = do
  remaining_init <- remainingM initial
  fmap2 unpack $ generalizedSearchM emptyLIFOHeap snd2 leastCostly nextM'
    (foundM . snd2) (remaining_init, (0, initial))
  where
    nextM' (_, (old_cost, old_st)) = do
      new_states <- Foldable.toList <$> nextM old_st
      sequence $ update_stateM <$> new_states
      where
        update_stateM new_st = do
          remaining <- remainingM new_st
          cost <- costM old_st new_st
          let new_cost = old_cost + cost
              new_est = new_cost + remaining
          return (new_est, (new_cost, new_st))
    unpack [] = (0, [])
    unpack packed_states =
      (fst . snd . last $ packed_states, map snd2 packed_states)
    snd2 = snd . snd

-- | @aStarAssocM@ is a monadic version of 'aStarAssoc': it has support for
-- monadic  @next@, @remaining@, and @found@ parameters.
aStarAssocM :: (Monad m, Num cost, Ord cost, Ord state)
  => (state -> m [(state, cost)])
  -- ^ function to generate list of neighboring states with associated
  -- transition costs given the current state
  -> (state -> m cost)
  -- ^ Estimate on remaining cost given a state
  -> (state -> m Bool)
  -- ^ Predicate to determine if solution found. 'aStarM' returns the shortest
  -- path to the first state for which this predicate returns 'True'.
  -> state
  -- ^ Initial state
  -> m (Maybe (cost, [state]))
  -- ^ (Total cost, list of steps) for the first path found which satisfies the
  -- given predicate
aStarAssocM nextM remainingM foundM initial = do
  remaining_init <- remainingM initial
  fmap2 unpack $ generalizedSearchM emptyLIFOHeap snd2 leastCostly nextM'
    (foundM . snd2) (remaining_init, (0, initial))
  where
    nextM' (_, (old_cost, old_st)) = do
      new_states <- nextM old_st
      sequence $ update_stateM <$> new_states
      where
        update_stateM new_st = do
          remaining <- remainingM (fst new_st)
          let new_cost = old_cost + (snd new_st)
              new_est = new_cost + remaining
          return (new_est, (new_cost, fst new_st))
    unpack [] = (0, [])
    unpack packed_states =
      (fst . snd . last $ packed_states, map snd2 packed_states)
    snd2 = snd . snd

-- | @incrementalCosts cost_fn states@ gives a list of the incremental costs
-- going from state to state along the path given in @states@, using the cost
-- function given by @cost_fn@. Note that the paths returned by the searches
-- in this module do not include the initial state, so if you want the
-- incremental costs along a @path@ returned by one of these searches, you
-- want to use @incrementalCosts cost_fn (initial : path)@.
--
-- === Example: Getting incremental costs from dijkstra
--
-- >>> import Data.Maybe (fromJust)
--
-- >>> :{
-- cyclicWeightedGraph :: Map.Map Char [(Char, Int)]
-- cyclicWeightedGraph = Map.fromList [
--   ('a', [('b', 1), ('c', 2)]),
--   ('b', [('a', 1), ('c', 2), ('d', 5)]),
--   ('c', [('a', 1), ('d', 2)]),
--   ('d', [])
--   ]
-- start = (0, 0)
-- end = (0, 2)
-- cost a b = fromJust . lookup b $ cyclicWeightedGraph Map.! a
-- :}
--
-- >>> incrementalCosts cost ['a', 'b', 'd']
-- [1,5]
incrementalCosts ::
  (state -> state -> cost)
  -- ^ Function to generate list of costs between neighboring states. This is
  -- only called for adjacent states in the `states` list, so it is safe to have
  -- this function be partial for non-neighboring states.
  -> [state]
  -- ^ A path, given as a list of adjacent states, along which to find the
  -- incremental costs
  -> [cost]
  -- ^ List of incremental costs along given path
incrementalCosts cost_fn states = zipWith cost_fn states (tail states)

-- | @incrementalCostsM@ is a monadic version of 'incrementalCosts': it has
-- support for a monadic @const_fn@ parameter.
incrementalCostsM ::
  (Monad m) =>
  (state -> state -> m cost)
  -- ^ Function to generate list of costs between neighboring states. This is
  -- only called for adjacent states in the `states` list, so it is safe to have
  -- this function be partial for non-neighboring states.
  -> [state]
  -- ^ A path, given as a list of adjacent states, along which to find the
  -- incremental costs
  -> m [cost]
  -- ^ List of incremental costs along given path
incrementalCostsM costM states = zipWithM costM states (tail states)

-- | @next \`pruning\` predicate@ streams the elements generate by @next@ into a
-- list, removing elements which satisfy @predicate@. This is useful for the
-- common case when you want to logically separate your search's `next` function
-- from some way of determining when you've reached a dead end.
--
-- === Example: Pruning a Set
--
-- >>> import qualified Data.Set as Set
--
-- >>> ((\x -> Set.fromList [0..x]) `pruning` even) 10
-- [1,3,5,7,9]
--
-- === Example: depth-first search, avoiding certain nodes
--
-- >>> import qualified Data.Map as Map
--
-- >>> :{
-- graph = Map.fromList [
--   ('a', ['b', 'c', 'd']),
--   ('b', [undefined]),
--   ('c', ['e']),
--   ('d', [undefined]),
--   ('e', [])
--   ]
-- :}
--
-- >>> dfs ((graph Map.!) `pruning` (`elem` "bd")) (== 'e') 'a'
-- Just "ce"
pruning ::
  (Foldable f)
  => (a -> f a)
  -- ^ Function to generate next states
  -> (a -> Bool)
  -- ^ Predicate to prune on
  -> (a -> [a])
  -- ^ Version of @next@ which excludes elements satisfying @predicate@
next `pruning` predicate =
  (filter (not . predicate) . Foldable.toList) <$> next

-- | @pruningM@ is a monadic version of 'pruning': it has support for monadic
-- @next@ and @predicate@ parameters
pruningM ::
  (Monad m, Foldable f)
  => (a -> m (f a))
  -- ^ Function to generate next states
  -> (a -> m Bool)
  -- ^ Predicate to prune on
  -> (a -> m [a])
  -- ^ Version of @next@ which excludes elements satisfying @predicate@
pruningM nextM predicateM a = do
  next_states <- nextM a
  filterM (fmap not. predicateM) $ Foldable.toList next_states

-- | A @SearchState@ represents the state of a generalized search at a given
-- point in an algorithms execution. The advantage of this abstraction is that
-- it can be used for things like bidirectional searches, where you want to
-- stop and start a search part-way through.
data SearchState container stateKey state = SearchState {
  current :: state,
  queue :: container,
  visited :: Set.Set stateKey,
  paths :: Map.Map stateKey [state]
  }

-- | Workhorse simple search algorithm, generalized over search container
-- and path-choosing function. The idea here is that many search algorithms are
-- at their core the same, with these details substituted. By writing these
-- searches in terms of this function, we reduce the chances of errors sneaking
-- into each separate implementation.
generalizedSearch ::
  (Foldable f, SearchContainer container, Ord stateKey, Elem container ~ state)
  => container
  -- ^ Empty @SearchContainer@
  -> (state -> stateKey)
  -- ^ Function to turn a @state@ into a key by which states will be compared
  -- when determining whether a state has be enqueued and / or visited
  -> ([state] -> [state] -> Bool)
  -- ^ Function @better old new@, which when given a choice between an @old@ and
  -- a @new@ path to a state, returns True when @new@ is a "better" path than
  -- old and should thus be inserted
  -> (state -> f state)
  -- ^ Function to generate "next" states given a current state
  -> (state -> Bool)
  -- ^ Predicate to determine if solution found. @generalizedSearch@ returns a
  -- path to the first state for which this predicate returns 'True'.
  -> state
  -- ^ Initial state
  -> Maybe [state]
  -- ^ First path found to a state matching the predicate, or 'Nothing' if no
  -- such path exists.
generalizedSearch empty mk_key better next found initial = runIdentity $
  generalizedSearchM empty mk_key better (Identity . next) (Identity . found) initial

-- | @nextSearchState@ moves from one @searchState@ to the next in the
-- generalized search algorithm
nextSearchStateM ::
  (Monad m, Foldable f, SearchContainer container, Ord stateKey,
   Elem container ~ state)
  => ([state] -> [state] -> Bool)
  -> (state -> stateKey)
  -> (state -> m (f state))
  -> SearchState container stateKey state
  -> m (Maybe (SearchState container stateKey state))
nextSearchStateM better mk_key nextM old = do
  (new_queue, new_paths) <- new_queue_paths_M
  let new_state_May = mk_search_state new_paths <$> pop new_queue
  case new_state_May of
    Just new_state ->
      if mk_key (current new_state) `Set.member` visited old
      then nextSearchStateM better mk_key nextM new_state
      else return (Just new_state)
    Nothing -> return Nothing
  where
    mk_search_state new_paths (new_current, remaining_queue) = SearchState {
      current = new_current,
      queue = remaining_queue,
      visited = Set.insert (mk_key new_current) (visited old),
      paths = new_paths
      }
    new_queue_paths_M =
      List.foldl' update_queue_paths (queue old, paths old)
        <$> nextM (current old)
    update_queue_paths (old_queue, old_paths) st =
      if mk_key st `Set.member` visited old
      then (old_queue, old_paths)
      else
        case Map.lookup (mk_key st) old_paths of
          Just old_path ->
            if better old_path (st : steps_so_far)
            then (q', ps')
            else (old_queue, old_paths)
          Nothing -> (q', ps')
        where
          steps_so_far = paths old Map.! mk_key (current old)
          q' = push old_queue st
          ps' = Map.insert (mk_key st) (st : steps_so_far) old_paths

-- | @generalizedSearchM@ is a monadic version of generalizedSearch
generalizedSearchM ::
  (Monad m, Foldable f, SearchContainer container, Ord stateKey,
   Elem container ~ state)
  => container
  -- ^ Empty @SearchContainer@
  -> (state -> stateKey)
  -- ^ Function to turn a @state@ into a key by which states will be compared
  -- when determining whether a state has be enqueued and / or visited
  -> ([state] -> [state] -> Bool)
  -- ^ Function @better old new@, which when given a choice between an @old@ and
  -- a @new@ path to a state, returns True when @new@ is a "better" path than
  -- old and should thus be inserted
  -> (state -> m (f state))
  -- ^ Function to generate "next" states given a current state
  -> (state -> m Bool)
  -- ^ Predicate to determine if solution found. @generalizedSearch@ returns a
  -- path to the first state for which this predicate returns 'True'.
  -> state
  -- ^ Initial state
  -> m (Maybe [state])
  -- ^ First path found to a state matching the predicate, or 'Nothing' if no
  -- such path exists.
generalizedSearchM empty mk_key better nextM foundM initial = do
  let initial_state =
        SearchState initial empty (Set.singleton $ mk_key initial)
        (Map.singleton (mk_key initial) [])
  end_May <- findIterateM (nextSearchStateM better mk_key nextM)
    (foundM . current) initial_state
  return $ fmap (reverse . get_steps) end_May
  where
    get_steps search_st = paths search_st Map.! mk_key (current search_st)

newtype LIFOHeap k a = LIFOHeap (Map.Map k [a])

emptyLIFOHeap :: LIFOHeap k a
emptyLIFOHeap = LIFOHeap Map.empty

-- | The @SearchContainer@ class abstracts the idea of a container to be used in
-- @generalizedSearch@
class SearchContainer container where
  type Elem container
  pop :: container -> Maybe (Elem container, container)
  push :: container -> Elem container -> container

instance SearchContainer (Seq.Seq a) where
  type Elem (Seq.Seq a) = a
  pop s =
    case Seq.viewl s of
      Seq.EmptyL -> Nothing
      (x Seq.:< xs) -> Just (x, xs)
  push s a = s Seq.|> a

instance SearchContainer [a] where
  type Elem [a] = a
  pop list =
    case list of
      [] -> Nothing
      (x : xs) -> Just (x, xs)
  push list a = a : list

instance Ord k => SearchContainer (LIFOHeap k a) where
  type Elem (LIFOHeap k a) = (k, a)
  pop (LIFOHeap inner)
    | Map.null inner = Nothing
    | otherwise = case Map.findMin inner of
      (k, [a]) -> Just ((k, a), LIFOHeap $ Map.deleteMin inner)
      (k, a : _) -> Just ((k, a), LIFOHeap $ Map.updateMin (Just . tail) inner)
      (_, []) -> pop (LIFOHeap $ Map.deleteMin inner)
                 -- Logically, this should never happen
  push (LIFOHeap inner) (k, a) = LIFOHeap $ Map.insertWith (++) k [a] inner

-- | @findIterateM@ is a monadic version of @findIterate@
findIterateM :: Monad m => (a -> m (Maybe a)) -> (a -> m Bool) -> a -> m (Maybe a)
findIterateM nextM foundM initial = do
  found <- foundM initial
  if found
  then return $ Just initial
  else nextM initial >>= maybe (return Nothing) (findIterateM nextM foundM)

-- | @leastCostly paths_a paths_b@ is a utility function to be used with
-- 'dijkstra'-like functions. It returns True when the cost of @paths_a@
-- is less than the cost of @paths_b@, where the total costs are the first
-- elements in each tuple in each path
leastCostly :: Ord a => [(a, b)] -> [(a, b)] -> Bool
leastCostly ((cost_a, _):_) ((cost_b, _):_) = cost_b < cost_a
-- logically this never happens, because if you have a
-- zero-length path a point, you already visited it
-- and thus do not consider other paths to it
leastCostly [] _ = False
-- logically this never happens, because you cannot find
-- a new zero-length path to a point
leastCostly _ [] = True

-- | This is just a convenience function which @fmap@s two deep
fmap2 :: (Functor f1, Functor f2) => (a -> b) -> f1 (f2 a) -> f1 (f2 b)
fmap2 = fmap . fmap


text:
{% extends "zinnia/base.html" %}
{% load i18n mptt_tags %}

{% block meta-description %}{% trans "Category list" %}{% endblock %}

{% block title %}{% trans "Categories" %}{% endblock %}

{% block body-class %}category-list{% endblock %}

{% block content %}

{% trans "Category list" %}

  {% recursetree object_list %}

    {{ node }}
    {% blocktrans count entry_count=node.entries_published.count %}{{ entry_count }} entry{% plural %}{{ entry_count }} entries{% endblocktrans %}
    {% if node.description %}
{{ node.description|striptags|safe }}
{% endif %}
    {% if not node.is_leaf_node %}

      {{ children }}

    {% endif %}

  {% endrecursetree %}
  {% if not object_list %}

    {% trans "No categories yet." %}

  {% endif %}

{% endblock %}

redpajama_set_name: RedPajamaGithub


text:
package com.sun.xacml.attr;

import java.net.URI;

import org.w3c.dom.Node;

import com.sun.xacml.ParsingException;

/**
 * Representation of an rfc822Name (ie, an email address).
 *
 * @since 1.0
 * @author Seth Proctor
 */
public class RFC822NameAttribute extends AttributeValue
{

    /**
     * Official name of this type
     */
    public static final String identifier =
        TypeIdentifierConstants.RFC822NAME;

    /**
     * URI version of name for this type
     *

     * This field is initialized by a static initializer so that
     * we can catch any exceptions thrown by URI(String) and
     * transform them into a RuntimeException, since this should
     * never happen but should be reported properly if it ever does.
     */
    private static URI identifierURI;

    /**
     * RuntimeException that wraps an Exception thrown during the
     * creation of identifierURI, null if none.
     */
    private static RuntimeException earlyException;

    /**
     * Static initializer that initializes the identifierURI
     * class field so that we can catch any exceptions thrown
     * by URI(String) and transform them into a RuntimeException.
     * Such exceptions should never happen but should be reported
     * properly if they ever do.
     */
    static {
        try {
            identifierURI = URI.create(identifier);
        } catch (IllegalArgumentException e) {
            earlyException = e;
        }
    }

    // the actual value being stored
    private String value;

    /**
     * Creates a new RFC822NameAttribute that represents the
     * value supplied.
     *
     * @param value the email address to be represented
     */
    public RFC822NameAttribute(String value) {
         super(identifierURI);

        // Shouldn't happen, but just in case...
        if (earlyException != null) {
            throw earlyException;
        }

        if (value == null) {
            throw new IllegalArgumentException("Can't create RFC822Name from"
                    + "null input");
        }
        // check that the string is an address, ie, that it has one and only
        // one '@' character in it
        String [] parts = value.split("@");
        if (parts.length != 2) {
            // this is malformed input
            throw new IllegalArgumentException("invalid RFC822Name: " + value);
        }

        // cannonicalize the name
        this.value = parts[0] + "@" + parts[1].toLowerCase();
    }

    /**
     * Returns a new RFC822NameAttribute that represents
     * the email address at a particular DOM node.
     *
     * @param root the Node that contains the desired value
     * @return a new RFC822NameAttribute representing the
     *         appropriate value
     * @throws ParsingException
     */
    public static RFC822NameAttribute getInstance(Node root)
            throws ParsingException {
        if (root.getFirstChild() != null) {
            return getInstance(root.getFirstChild().getNodeValue());
        }
        throw new ParsingException("Error while parsing"
                + "a RFC822NameAttribute");
    }

    /**
     * Returns a new RFC822NameAttribute that represents
     * the email address value indicated by the string provided.
     *
     * @param value a string representing the desired value
     * @return a new RFC822NameAttribute representing the
     *         appropriate value
     */
    public static RFC822NameAttribute getInstance(String value) {
        return new RFC822NameAttribute(value);
    }

    /**
     * Returns the name value represented by this object
     *
     * @return the name
     */
    public String getValue() {
        return this.value;
    }

    /**
     * Returns true if the input is an instance of this class and if its
     * value equals the value contained in this class.
     *
     * @param o the object to compare
     *
     * @return true if this object and the input represent the same value
     */
    public boolean equals(Object o) {
        if (! (o instanceof RFC822NameAttribute)) {
            return false;
        }
        RFC822NameAttribute other = (RFC822NameAttribute)o;

        return this.value.equals(other.value);
    }

    /**
     * Returns the hashcode value used to index and compare this object with
     * others of the same type. Typically this is the hashcode of the backing
     * data object.
     *
     * @return the object's hashcode value
     */
    public int hashCode() {
        return this.value.hashCode();
    }

    /**
     * @return  The String encoding this AttributeValue.
     */
    public String encode() {
        return this.value;
    }

}


text:
<!DOCTYPE html>

	<meta name="google-site-verification" content="sWjPUl8Pny1_EElYjJYqS30XUO2KHY__Q5b08z2k5U4" />
	<meta name="google-site-verification" content="njR3jvsK6MFNRgsHpDm0YpzOjefp4R4oxniRHOjhao8" />
	<meta name="msvalidate.01" content="235F405786FAB553A2A8EF5FD13514A7" />
	<meta name="p:domain_verify" content="pinterest-cd4ab.html" />
	<meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
	<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
	<title>Navy.mil The Official Website of the United States Navy: Home Page</title>
	<meta name="description" content="The Official Website of the United States Navy: US Navy News Stories, Photos, Videos and Social Media">
	<meta name="author" content="">
	<meta name="viewport" content="width=device-width">

    <meta property="fb:pages" content="74281347822" />
    <meta property="fb:app_id" content="985113944899931" />
	<link rel="shortcut icon" href="/favicon.ico">
	<link rel="apple-touch-icon" href="/apple-touch-icon.png">

	<link href='http://fonts.googleapis.com/css?family=Oswald' rel='stylesheet' type='text/css'>

	<link rel="stylesheet" href="/css/960_24_col.css">
	<link rel="stylesheet" href="/css/siteStyle.css">
	<link rel="stylesheet" href="/css/index.css">
	<link rel="stylesheet" href="/css/indexHeaderFooter.css">
	<link rel="stylesheet" href="/css/indexMainSlider.css" >
	<link rel="stylesheet" href="/css/indexPhotoSlider.css">
	<link rel="stylesheet" media="handheld" href="/css/handheld.css">

	Skip to Content

Official Website of the United States Navy

United States of America Department of the Navy Seal

			Navy.mil Underway
			<form id="main-search" method=get accept-charset="UTF-8" action="http://search.navy.mil/search">
				<input type="text" id="query" name="query" autocomplete="off" value="Search Navy.mil" title="Search Navy.mil" onFocus="clearText(this);">
				<input name=btnG id="submit-btn" title="Go Search" type="submit" value=" ">
				<input name="utf8" type="hidden" value="✓" />
				<input name="affiliate" type="hidden" value="navy_all" />
				</form>

Facebook

Twitter

Blog Feed

YouTube

Flickr

Pinterest

Instagram

Home

About

Status of the Navy

Navy Organization

Strategic Documents

Command Directory

Our Ships

Fact Files

Today in Naval History

Contact Us

Command Addresses (SNDL)

FAQ

Leadership

Secretary of the Navy (Acting)

Chief Of Naval Operations

Master Chief Petty Officer Of The Navy

Chief Of Naval Personnel

Biographies

SES Biographies

News

Top Stories

Headlines

Feature Stories

Local Stories

Media

Video Gallery

Photo Gallery

Featured Galleries

All Hands Magazine

All Hands Update

All Hands Archive

Graphics Gallery

Art Gallery

Podcasts & RSS Feeds

2015 Owners and Operators

All Hands Radio

Links

Navy Reserve

Navy.mil Underway

Navy Personnel Command (NPC)

Navy SAPR

Navy EEO

Navy Standard Integrated Personnel System (NSIPS)

Navy Knowledge Online

Board of Corrections for Naval Records (BCNR)

Shift Colors-Navy Retiree Newsletter

Civilian Federal Careers

Navy / Marine Corps Relief

Affected Navy Family Assistance

Operations Security (OPSEC)

Navy Trademarks

Military One Source

USA.gov

U.S. Office of Special Counsel

Social Media Directory and Policy

US Navy App Locker

NCIS Report a Crime

Medal of Honor, USN Recipients

Joint Chiefs of Staff

Defense.gov

U.S. Army

U.S. Air Force

U.S. Marine Corps

U.S. Coast Guard

Naval History & Heritage Command

Navy Office of Community Outreach

Careers

Military Careers

Civilian Careers

Navy POD

Month in Review

Month in Review June 2017. A look back at your Navy in Action. Photos and video submitted by the fleet. Submit your photos via www.navy.mil. View Video

Warrior Games Team Navy

Team Navy is comprised of 36 Sailors and four Coast Guardsmen who are competing June 30 to July 8 against other military teams to compete for gold, silver and bronze medals in archery, cycling, field, shooting, sitting volleyball, swimming, track and wheelchair basketball.Learn More

FY18 Chief Quotas

The cycle 234 chief petty officer selection board convened June 26. Over 18,000 active duty E-6 Sailors' records are before the board that will select up to 4, 357 to advance to E-7.
View Quotas

Physiological Episodes

CNO Adm. John Richardson and the Naval Aviation Enterprise consider the physiological episodes (PEs) in F/A-18 variants and T-45s their number one safety priority.

 Visit Navy PEs PageView Video

Navy News Service Top Stories Top Stories RSS Feed

FY-18 Senior Enlisted Continuation Board Convenes in December

The fiscal year 2018 (FY-18) Senior Enlisted Continuation Board (SECB) will convene on December 4, according to NAVADMIN 167/17 released July 10.

					<time datetime="7/12/2017 10:38:00 AM">July 12, 2017</time>

Navy Announces Review of All Collateral Duties, Eliminates Seven Now

The Navy eliminated seven shipboard collateral duties in NAVADMIN 168/17, released July 11, and tasked a working group to further review all collateral duties for the potential to be eliminated or consolidated.

					<time datetime="7/11/2017 9:38:00 AM">July 11, 2017</time>

Nimitz Carrier Strike Group Begins Malabar 2017

Sailors and Marines assigned to the Nimitz Carrier Strike Group, the Indian Navy and the Japan Maritime Self-Defense Force (JMSDF) commenced the Malabar in-port portions in Chennai, India, July 9.

					<time datetime="7/10/2017 2:22:00 PM">July 10, 2017</time>

Access the U.S. Navy App Locker

Source for Information and Download Links for Mobile Apps Developed by the U.S. Navy

				Subscribe to Navy News Service

ALL HANDS UPDATE HEADLINES AHU RSS Feed

July 11, 2017

Nimitz Carrier Strike Group Begins Malabar 2017, DANTES Releases "MyVolEdPath" Voluntary Education Mobile App, America Amphibious Readiness Group Departs for Scheduled Deployment.

Headlines from Around the Fleet Other Headlines RSS Feed

NAVFAC Marianas Engineers Partner with SAME Guam Post; Host First STEM Camp on Island

Sea Breeze 2017; Improving MOC Processes

NHCP Eagles Win Camp Pendleton CG Cup Soccer League

JCS Sailors Go Full Speed Ahead

Vouchers Available to Accelerate Officer Graduate Education

USNS Lewis B. Puller Begins First Operational Deployment

NCBC Gulfport Celebrates Completion of Four Megawatt Solar Facility

Warfare Center Working Group Quickly Meets NAVAIR Requirement with 3-D Printers

International Partner Visits Dahlgren Training Command

Iwo Jima ARG Departs for PMINT

Ike Hosts Friends, Family Day Cruise

USS Hue City and USS Carney Arrive in Odessa, Ukraine

Truxtun Visits Souda Bay

USS Carney Enters Black Sea

USS Hue City Enters Black Sea

				More Headlines...

Images of the Day Images of the Day RSS Feed

					  Photo Gallery
					  Top Images

							<img src="/management/photodb/webphoto/web_170711-N-WC492-030.jpg" data-id="241617" data="YOKOSUKA, Japan (July 11, 2017)" alt="The Arleigh Burke-class guided-missile destroyer USS Fitzgerald (DDG 62) sits in Dry Dock 4 at Fleet Activities (FLEACT) Yokosuka to continue repairs and assess damage sustained from its June 17 collision with a merchant vessel. FLEACT Yokosuka provides, maintains, and operates base facilities and services in support of U.S. 7th Fleet's forward-deployed naval forces, 71 tenant commands and 26,000 military and civilian personnel.  U.S. Navy photo by Daniel A. Taylor (Released)  170711-N-WC492-030"/>

							<img src="/management/photodb/webphoto/web_170711-N-PD309-023.jpg" data-id="241615" data="SOUTH CHINA SEA (July 11, 2017)" alt="The littoral combat ship USS Coronado (LCS 4) fires a Mark 110 57 mm gun during a live-fire exercise. Coronado is on a rotational deployment in U.S. 7th Fleet area of responsibility, patrolling the region's littorals and working hull-to-hull with partner navies to provide the U.S. 7th Fleet with the flexible capabilities it needs now and in the future.  U.S. Navy photo by Mass Communication Specialist 3rd Class Deven Leigh Ellis (Released)  170711-N-PD309-023"/>

							<img src="/management/photodb/webphoto/web_170710-N-PD309-118.jpg" data-id="241614" data="SOUTH CHINA SEA (July 10, 2017)" alt="Members of the Vietnam People's Navy wave goodbye to the littoral combat ship USS Coronado (LCS 4) after an exchange during Naval Engagement Activity (NEA) Vietnam 2017. The engagement provides an opportunity for Sailors from the U.S. Navy and Vietnam People's Navy to interact and share knowledge to enhance mutual capabilities and strengthen solid partnerships.  U.S. Navy photo by Mass Communication Specialist 3rd Class Deven Leigh Ellis (Released)  170710-N-PD309-118"/>

							<img src="/management/photodb/webphoto/web_170711-N-UK248-0135.jpg" data-id="241616" data="PACIFIC OCEAN (July 11, 2017)" alt="Aviation Electronics Technician 1st Class Michael Moore, assigned to the amphibious assault ship USS America (LHA 6), troubleshoots the Consolidated Automatic Support System (CASS) in the ship's CASS shop. America is on deployment with its amphibious ready group comprised of more than 1,800 Sailors and 2,600 Marines assigned to the amphibious dock landing ship USS Pearl Harbor (LSD 52), the amphibious transport dock ship USS San Diego (LPD 22) and America.  U.S. Navy photo by Mass Communication Specialist 2nd Class Kristina Young (Released)  170711-N-UK248-0135"/>

location

						View Caption
						Hide Caption

				Stay Connected #USNavy

				Tweets by @USNavy

					Navy Live

@USNPeople Weekly Wire Rundown: July 10, 2017

Families and Caregivers: Essential element in athletes recovery and rehabilitation

Service Dogs: More than Mans Best Friend for Team Navy Athletes

Command and Social Media Directory

					Find my Command, Registration, Guidance and Best Practices

Strategic Documents
SECNAV Transcripts, CNO Transcripts, and more.

Home

About

Status of the Navy

Navy Organization

Strategic Documents

Command Directory

Our Ships

Fact Files

Today in Naval History

Contact Us

Command Addresses (SNDL)

FAQ

Leadership

Secretary of the Navy (Acting)

Chief Of Naval Operations

Master Chief Petty Officer Of The Navy

Chief Of Naval Personnel

Biographies

SES Biographies

News

Top Stories

Headlines

Feature Stories

Local Stories

Media

Video Gallery

Photo Gallery

Featured Galleries

All Hands Magazine

All Hands Update

All Hands Archive

Graphics Gallery

Art Gallery

Podcasts & RSS Feeds

2015 Owners and Operators

All Hands Radio

Links

Navy Reserve

Navy.mil Underway

Navy Personnel Command (NPC)

Navy SAPR

Navy EEO

Navy Standard Integrated Personnel System (NSIPS)

Navy Knowledge Online

Board of Corrections for Naval Records (BCNR)

Shift Colors-Navy Retiree Newsletter

Civilian Federal Careers

Navy / Marine Corps Relief

Affected Navy Family Assistance

Operations Security (OPSEC)

Navy Trademarks

Military One Source

USA.gov

U.S. Office of Special Counsel

Social Media Directory and Policy

US Navy App Locker

NCIS Report a Crime

Medal of Honor, USN Recipients

Joint Chiefs of Staff

Defense.gov

U.S. Army

U.S. Air Force

U.S. Marine Corps

U.S. Coast Guard

Naval History & Heritage Command

Navy Office of Community Outreach

Careers

Military Careers

Civilian Careers

Navy POD

Official Website of the United States Navy

Chief of Information

Attn: US Navy

1200 Navy Pentagon

Washington DC 20350-1200

FOIA - Privacy Policy/External Links - No Fear Act
Accessibility/Section 508


text:
var MongoClient=require('mongodb').MongoClient,
settings=require('./setting'),
stock=require('./stockData'),
color=require('colors'),
fs=require('fs'),
rl=require('readline');
var reader = rl.createInterface({
    input: process.stdin,
    output: process.stdout
});
var async = require('async');

exports.initialDb=function(callback){
    MongoClient.connect("mongodb://localhost/"+settings.db,function(err,db){
			if(err){
				callback(undefined);
				return;
			}
			callback(db);
			return;
	});
}

//証券コード一覧表のjsonファイルをデータベースに格納する
//collection名 : stockCode
//データ構造     : {"stockCode":int,"stockName":String}
exports.insertStockCode=function(fileName){
	var stockCodeArray=[];
    MongoClient.connect("mongodb://localhost/"+settings.db,function(err,db){
		if(err)console.error(err.yellow);
		console.log("データベース接続完了");
		db.collection("stockCode",function(err,collection){
			collection.drop(); //古いDBの削除
			console.log("ファイル読み取り開始".yellow);
			var inputStream = fs.createReadStream(fileName);
	    	var inputReadLine = rl.createInterface({'input': inputStream, 'output': {}});
	    	inputReadLine
			.on('line', function(line){
		    	console.error(line.yellow);
		    	stockCodeArray.push(JSON.parse(line));
			})
			.on('close', function() {
		    	console.error("読み取り終了".yellow);
				collection.insert(stockCodeArray,function(err,result){
					if(err)console.error("データベース格納時にエラー発生しました".yellow);
					console.error("データベース格納成功".yellow);
					db.close();
		    	});
			});
		});
    });
}

//証券コードに対応する銘柄を検索する
//対応する番号がないときはNullを帰す
exports.getStockName=function(stockNumber,callback){
	MongoClient.connect("mongodb://localhost/"+settings.db,function(err,db){
		if(err)console.error(err.yellow);
		console.error("[mongo.js:getStockName] データベース接続完了".yellow);
		db.collection("stockCode",function(err,collection){
			 collection.find({"stockCode":Number(stockNumber)}).toArray(function(err, docs) {
    			if(err || docs.length==0){
					db.close();
					console.error("[mongo.js:getStockName] 一致する銘柄が見つかりませんでした..".yellow);
					callback(null);
					return;
				}
				db.close();
				console.error("[mongo.js:getStockName] 証券番号 : ".yellow+stockNumber.yellow+",".yellow+"銘柄名 : ".yellow+docs[0].stockName.yellow);
    			callback(docs[0].stockName);
				return;
			 });
  		});
	});
}

exports.insertDb=function(stockNumber,stockArray,callback){
    MongoClient.connect("mongodb://localhost/"+settings.db,function(err,db){
	if(err)console.error(err.yellow);
	console.error("証券番号".yellow+stockNumber.yellow+"データベース接続完了".yellow);
	db.collection("s"+stockNumber,function(err,collection){
		if(err){
			callback(undefined);
			return;
		}
	    collection.insert(stockArray,function(err,result){
	      	if(err)console.error("データベース格納時にエラー発生しました".yellow);
 	     	console.error("証券番号".yellow+stockNumber.yellow+"データベース格納成功".yellow);
			db.close();
			callback("end");
			return;
 	    });
	});
    });
}

exports.sortDb=function(stockNumber,sortType){
    MongoClient.connect("mongodb://localhost/"+settings.db,function(err,db){
	if(err)console.log(err);
	//console.log("connected to db");
	db.collection("s"+stockNumber,function(err,collection){
	    //var array=stock.getStockTimeSeries("9501",stock.getNumberOfPages("9501"));
	    var stream=collection.find().sort({'HP':sortType}).stream();
	    stream.on("data",function(item){
		console.log("%j",item);
	    });
	    stream.on("end",function(item){
		console.error("end".yellow);
		db.close();
	    });
	});
    });
}

exports.getBeginYMD=function(collection,callback){
	collection.find().sort({"DATE":1}).limit(1).toArray(function(err,docs){
		console.log("[mongo.js : getBeginYMD]"+docs);
		if(docs.length!=1){
			console.error("[mongo.js : getBeginYMD] 配列長にエラー");
			callback(undefined);
			return;
		}
		callback({"Y":docs[0].Y,"M":docs[0].M,"D":docs[0].D});
		return;
	});
}

exports.getLatestYMD=function(collection,callback){
	collection.find().sort({"DATE":-1}).limit(1).toArray(function(err,docs){
		console.log("[mongo.js : getBeginYMD]"+docs);
		if(docs.length!=1){
			console.error("[mongo.js : getBeginYMD] 配列長にエラー");
			callback(undefined);
			return;
		}
		callback({"Y":docs[0].Y,"M":docs[0].M,"D":docs[0].D});
		return;
	});
}

exports.findBySpan=function(arraySpanYMD,collection,callback){
	var array=[];
	async.forEachSeries(arraySpanYMD, function(val,cb) {
		if(val.SM<10)val.SM='0'+val.SM;
		if(val.SD<10)val.SD='0'+val.SD;
		if(val.EM<10)val.EM='0'+val.EM;
		if(val.ED<10)val.ED='0'+val.ED;
		var param={"DATE":{$gte:val.SY+"-"+val.SM+"-"+val.SD,$lte:val.EY+"-"+val.EM+"-"+val.ED}};
		//console.log("[mongo.js findBySpan]%j",param);
		collection.find(param).sort({DATE:-1}).toArray(function(err,docs){
			//console.log("[findBySpan]"+docs.length);
			array.push(docs);
			cb();
		});

	}, function() {
		//console.log(array);
  		callback(array);
		return;
	});
}


text:
package org.realityforge.gwt.datatypes.client.collections;

import com.google.gwt.core.client.JavaScriptObject;
import com.google.gwt.core.client.JsArray;
import java.util.AbstractSet;
import java.util.Iterator;
import java.util.Set;

/**
 * An adapter converting the JsArray to support a Set interface.
 */
public final class JsoReadOnlySet<E extends JavaScriptObject>
  extends AbstractSet<E>
  implements Set<E>
{
  private final JsArray<E> _data;

  public JsoReadOnlySet( final JsArray<E> data )
  {
    _data = data;
  }

  public JsArray<E> asArray()
  {
    return _data;
  }

  @Override
  public Iterator<E> iterator()
  {
    return new JsoIterator<E>( this );
  }

  @Override
  public int size()
  {
    return _data.length();
  }

  static class JsoIterator<E extends JavaScriptObject>
    implements Iterator<E>
  {
    private JsoReadOnlySet<E> _set;
    private int _offset;

    JsoIterator( final JsoReadOnlySet<E> set )
    {
      _set = set;
    }

    @Override
    public boolean hasNext()
    {
      return _offset < _set.size();
    }

    @Override
    public E next()
    {
      return _set._data.get( _offset++ );
    }

    @Override
    public void remove()
    {
      throw new UnsupportedOperationException();
    }
  }
}


text:
leftNumber = process.argv[2];
operation = process.argv[3];
rightNumber = process.argv[4];

if (typeof(leftNumber) === "undefined" || leftNumber === null || leftNumber === "") {
    leftNumber = 0;
}

if (typeof(operation) === "undefined" || operation === null || operation === "") {
    leftNumber = "";
}

if (typeof(rightNumber) === "undefined" || rightNumber === null || rightNumber === "") {
    rightNumber = 0;
}

if (operation === "") {
    console.log("result: nop");
}
else {
    if (operation === "+") {
        result = ((1 * leftNumber) + (1 * rightNumber));
        console.log("result: " + result);
    }
    else if (operation === "-") {
        result = leftNumber - rightNumber;
        console.log("result: " + result);
    }
    else if (operation === "*") {
        result = leftNumber * rightNumber;
        console.log("result: " + result);
    }
    else if (operation === "/") {
        result = leftNumber / rightNumber;
        console.log("result: " + result);
    }
    else if (operation === "%") {
        result = leftNumber % rightNumber;
        console.log(result);
    }
    else if (operation === "bin") {
        base = 2;
        limit = base - 1;

        nextNumber = leftNumber;
        conversionArray = new Array();
        while (nextNumber >= limit) {
            conversionArray.push(nextNumber % base);
            nextNumber = Math.floor(nextNumber / base);
        }

        len = conversionArray.length;
        result = "";
        for (var i = 0; i < len; i++) {
            result = result + conversionArray[i];
        }

        console.log("result: " + result)
    }
}


text:
<?php

/**
 * Zend_Mail_Protocol_Smtp
 */
require_once 'Zend/Mail/Protocol/Smtp.php';

/**
 * Zend_Mail_Transport_Smtp
 */
require_once 'Zend/Mail/Transport/Smtp.php';

/**
 * @category   Zend
 * @package    Zend_Mail
 * @subpackage UnitTests
 * @copyright  Copyright (c) 2005-2012 Zend Technologies USA Inc. (http://www.zend.com)
 * @license    http://framework.zend.com/license/new-bsd     New BSD License
 * @group      Zend_Mail
 */
class Zend_Mail_SmtpTest extends PHPUnit_Framework_TestCase
{
    protected $_params;
    protected $_transport;
    protected $_connection;

    public function setUp()
    {
        $this->_params = array('host'     => TESTS_ZEND_MAIL_SMTP_HOST,
                               'port'     => TESTS_ZEND_MAIL_SMTP_PORT,
                               'username' => TESTS_ZEND_MAIL_SMTP_USER,
                               'password' => TESTS_ZEND_MAIL_SMTP_PASSWORD,
                               'auth'     => TESTS_ZEND_MAIL_SMTP_AUTH);
    }

    public function testTransportSetup()
    {
        try {
            $this->_transport = new Zend_Mail_Transport_Smtp($this->_params['host'], $this->_params);
        } catch (Exception $e) {
            $this->fail('exception raised while creating smtp transport');
        }

        try {
            $this->_connection = new Zend_Mail_Protocol_Smtp($this->_params['host'], $this->_params['port']);
            $this->_transport->setConnection($this->_connection);
        } catch (Exception $e) {
            $this->fail('exception raised while setting smtp transport connection');
        }

        $this->_connection = $this->_transport->getConnection();
        if (!($this->_connection instanceof Zend_Mail_Protocol_Abstract)) {
            $this->fail('smtp transport connection is not an instance of protocol abstract');
        }
    }
}


text:
class ClientRunner : public QObject {

    Q_OBJECT

public:

 Q_INVOKABLE void start() {

        // Create client
        BitCoindRPC::Client client("127.0.0.1",
                                 8332,
                                 "bitcoinrpc",
                                 "DDKVyZDNros2cKvkk5KpGmJWGazzYMezoWTeKaXcqxEj",
                                 "*");

        // Make request
        qDebug() << client.getBalanceBlocking();

    }
};

int main(int argc, char *argv[]) {

    QCoreApplication a(argc, argv);

    ClientRunner * r = new ClientRunner();

    //QMetaObject::invokeMethod(&r, "start");

    return a.exec();
}


text:
'use strict';

angular.module('iadssWebApp')
  .controller('MainCtrl', ['$scope', function ($scope) {
    $scope.contributors = [
        { name: "G. Crofts, PhD", association: "School of Health Sciences, University of Salford", country: "UK" },
        { name: "R. Padman, PhD", association: "The H. John Heinz III College, Carnegie Mellon University", country: "USA" },
        { name: "O. Alani, PhD", association: "School of Computing, Science and Engineering, University of Salford", country: "UK" },
        { name: "F. Meziane, PhD", association: "School of Computing, Science and Engineering, University of Salford", country: "UK" },
        { name: "N. Maharaja", association: "The H. John Heinz III College, Carnegie Mellon University", country: "USA" },
        { name: "B.J. Kolowitz, DSc", association: "UPMC", country: "USA" }
    ];
}]);


text:
#include "Arduino.h"

#ifdef __cplusplus
extern "C" {
#endif

/*
 * System Core Clock is at 1MHz (8MHz/8) at Reset.
 * It is switched to 48MHz in the Reset Handler (startup.c)
 */
uint32_t SystemCoreClock=1000000ul ;

/*
void calibrateADC()
{
  volatile uint32_t valeur = 0;

  for(int i = 0; i < 5; ++i)
  {
    ADC->SWTRIG.bit.START = 1;
    while( ADC->INTFLAG.bit.RESRDY == 0 || ADC->STATUS.bit.SYNCBUSY == 1 )
    {
      // Waiting for a complete conversion and complete synchronization
    }

    valeur += ADC->RESULT.bit.RESULT;
  }

  valeur = valeur/5;
}*/

/*
 * Arduino Zero board initialization
 *
 * Good to know:
 *   - At reset, ResetHandler did the system clock configuration. Core is running at 48MHz.
 *   - Watchdog is disabled by default, unless someone plays with NVM User page
 *   - During reset, all PORT lines are configured as inputs with input buffers, output buffers and pull disabled.
 */
void init( void )
{
  // Set Systick to 1ms interval, common to all Cortex-M variants
  if ( SysTick_Config( SystemCoreClock / 1000 ) )
  {
    // Capture error
    while ( 1 ) ;
  }
  NVIC_SetPriority (SysTick_IRQn,  (1 << __NVIC_PRIO_BITS) - 2);  /* set Priority for Systick Interrupt (2nd lowest) */

  // Clock PORT for Digital I/O
//  PM->APBBMASK.reg |= PM_APBBMASK_PORT ;
//
//  // Clock EIC for I/O interrupts
//  PM->APBAMASK.reg |= PM_APBAMASK_EIC ;

  // Clock SERCOM for Serial
  PM->APBCMASK.reg |= PM_APBCMASK_SERCOM0 | PM_APBCMASK_SERCOM1 | PM_APBCMASK_SERCOM2 | PM_APBCMASK_SERCOM3 | PM_APBCMASK_SERCOM4 | PM_APBCMASK_SERCOM5 ;

  // Clock TC/TCC for Pulse and Analog
  PM->APBCMASK.reg |= PM_APBCMASK_TCC0 | PM_APBCMASK_TCC1 | PM_APBCMASK_TCC2 | PM_APBCMASK_TC3 | PM_APBCMASK_TC4 | PM_APBCMASK_TC5 ;

  // ATSAMR, for example, doesn't have a DAC
#ifdef PM_APBCMASK_DAC
  // Clock ADC/DAC for Analog
  PM->APBCMASK.reg |= PM_APBCMASK_ADC | PM_APBCMASK_DAC ;
#endif

  // Setup all pins (digital and analog) in INPUT mode (default is nothing)
  for (uint32_t ul = 0 ; ul < NUM_DIGITAL_PINS ; ul++ )
  {
    pinMode( ul, INPUT ) ;
  }

  // Initialize Analog Controller
  // Setting clock
  while(GCLK->STATUS.reg & GCLK_STATUS_SYNCBUSY);

  GCLK->CLKCTRL.reg = GCLK_CLKCTRL_ID( GCM_ADC ) | // Generic Clock ADC
                      GCLK_CLKCTRL_GEN_GCLK0     | // Generic Clock Generator 0 is source
                      GCLK_CLKCTRL_CLKEN ;

  while( ADC->STATUS.bit.SYNCBUSY == 1 );          // Wait for synchronization of registers between the clock domains

  ADC->CTRLB.reg = ADC_CTRLB_PRESCALER_DIV512 |    // Divide Clock by 512.
                   ADC_CTRLB_RESSEL_10BIT;         // 10 bits resolution as default

  ADC->SAMPCTRL.reg = 0x3f;                        // Set max Sampling Time Length

  while( ADC->STATUS.bit.SYNCBUSY == 1 );          // Wait for synchronization of registers between the clock domains

  ADC->INPUTCTRL.reg = ADC_INPUTCTRL_MUXNEG_GND;   // No Negative input (Internal Ground)

  // Averaging (see datasheet table in AVGCTRL register description)
  ADC->AVGCTRL.reg = ADC_AVGCTRL_SAMPLENUM_1 |    // 1 sample only (no oversampling nor averaging)
                     ADC_AVGCTRL_ADJRES(0x0ul);   // Adjusting result by 0

  analogReference( AR_DEFAULT ) ; // Analog Reference is AREF pin (3.3v)

  // Initialize DAC
  // Setting clock
  while ( GCLK->STATUS.reg & GCLK_STATUS_SYNCBUSY );
  GCLK->CLKCTRL.reg = GCLK_CLKCTRL_ID( GCM_DAC ) | // Generic Clock ADC
                      GCLK_CLKCTRL_GEN_GCLK0     | // Generic Clock Generator 0 is source
                      GCLK_CLKCTRL_CLKEN ;

 // ATSAMR, for example, doesn't have a DAC
#ifdef DAC
  while ( DAC->STATUS.bit.SYNCBUSY == 1 ); // Wait for synchronization of registers between the clock domains
  DAC->CTRLB.reg = DAC_CTRLB_REFSEL_AVCC | // Using the 3.3V reference
                   DAC_CTRLB_EOEN ;        // External Output Enable (Vout)
#endif

}

#ifdef __cplusplus
}
#endif


text:
(function () {
    'use strict';
    //REQUIRES
    var express = require('express');
    var controller = require('./user.ctrl');
    var config = require('../../config/environment');
    var auth = require('../../auth/auth.service');

    var router = express.Router();

    /////ENDPOINTS FOR USERS/////

    router.get('/', auth.hasRole('admin'), controller.index);//GET ALL USERS FROM DATABASE
    router.delete('/:id', auth.hasRole('admin'), controller.destroy);//DELETE SPECIFIC USER FROM DATABASE
    router.get('/me', auth.isAuthenticated(), controller.me);//
    router.put('/:id/password', auth.isAuthenticated(), controller.changePassword);
    router.get('/:id', auth.isAuthenticated(), controller.show);
    router.post('/', controller.create);
    router.get('/userInfo/:memberId', controller.findByID);//FIND SPECIFIC GYM
    router.get('/gym/:gymID', controller.getUsersByGym);//GET ALL USERS FROM SPECIFIC GYM
    router.get('/details/:memberId', controller.getUserDetails);//GET SPECIFIC USER DETAILS
    router.get('/logos/:memberId', controller.getUserLogos);//GET LOGOS PATHWAY FOR SPECIFIC USER
    router.get('/pathos/:memberId', controller.getUserPathos);//GET PATHOS PATHWAY FOR SPECIFIC USER
    router.get('/ethos/:memberId', controller.getUserEthos);//GET ETHOS PATHWAY FOR SPECIFIC USER
    router.put('/updateEval', controller.updateEvalStatus);//UPDATE EVALUATION WHEN CHANGES HAPPEN ON FRONT
    router.put('/updateAnswer', controller.updateAnswer);//UPDATE ANSWERS FROM KNOWLEDGE QUESTIONS
    router.put('/updateProgression', controller.updateProgression);//UPDATE OR ADD PROGRESSIONS FOR PHYSICAL ON FRONT
    router.put('/update', controller.updateUser);//GENERAL UPDATE USER ENDPOINT
    router.put('/update/isadmin', controller.userIsAdminUpdate);//UPDATE WHETHER USER IS ADMIN
    router.put('/update/isactive', controller.userIsActiveUpdate);//UPDATE WHETHER USER IS ACTIVE

    module.exports = router;

} ());


text:
* [willowtreeapps / spruce-ios](https://github.com/willowtreeapps/spruce-ios):
        Swift library for choreographing animations on the screen.

* [zixun / GodEye](https://github.com/zixun/GodEye):
        Automaticly display Log,Crash,Network,ANR,Leak,CPU,RAM,FPS,NetFlow,Folder and etc with one line of code based on Swift. Just like God opened his eyes

* [uraimo / Awesome-Swift-Playgrounds](https://github.com/uraimo/Awesome-Swift-Playgrounds):
        A List of Awesome Swift Playgrounds

* [ChiliLabs / CHIPageControl](https://github.com/ChiliLabs/CHIPageControl):
        A set of cool animated page controls written in Swift to replace boring UIPageControl.

* [Nightonke / Gitee](https://github.com/Nightonke/Gitee):
        Gitee, OS X status bar application for Github

* [ispiropoulos / PRGRoundSlider](https://github.com/ispiropoulos/PRGRoundSlider):
        A fully customizable circular slider

* [IvanVorobei / RequestPermission](https://github.com/IvanVorobei/RequestPermission):
        simple permission request with beautiful UI

* [marty-suzuki / ReverseExtension](https://github.com/marty-suzuki/ReverseExtension):
        UITableView extension that enabled to insert cell from bottom of tableView.

* [ReactiveX / RxSwift](https://github.com/ReactiveX/RxSwift):
        Reactive Programming in Swift

* [louisdh / panelkit](https://github.com/louisdh/panelkit):
        A UI framework that enables panels on iOS.

* [JohnSundell / Marathon](https://github.com/JohnSundell/Marathon):
        Marathon makes it easy to write, run and manage your Swift scripts

* [lkzhao / Hero](https://github.com/lkzhao/Hero):
        Elegant transition library for iOS & tvOS

* [vsouza / awesome-ios](https://github.com/vsouza/awesome-ios):
        A curated list of awesome iOS ecosystem, including Objective-C and Swift Projects

* [fmo91 / PluggableApplicationDelegate](https://github.com/fmo91/PluggableApplicationDelegate):
        Smallest AppDelegate ever by using a decoupled-services based architecture. 🛠

* [msaps / Pageboy](https://github.com/msaps/Pageboy):
        A simple, highly informative page view controller.

* [raywenderlich / swift-algorithm-club](https://github.com/raywenderlich/swift-algorithm-club):
        Algorithms and data structures in Swift, with explanations!

* [nodes-ios / Serpent](https://github.com/nodes-ios/Serpent):
        A protocol to serialize Swift structs and classes for encoding and decoding.

* [danielgindi / Charts](https://github.com/danielgindi/Charts):
        Beautiful charts for iOS/tvOS/OSX! The Apple side of the crossplatform MPAndroidChart.

* [jerkoch / SwipeCellKit](https://github.com/jerkoch/SwipeCellKit):
        Swipeable UITableViewCell based on the stock Mail.app, implemented in Swift.

* [shadowsocks / ShadowsocksX-NG](https://github.com/shadowsocks/ShadowsocksX-NG):
        Next Generation of ShadowsocksX

* [PerfectlySoft / Perfect](https://github.com/PerfectlySoft/Perfect):
        Server-side Swift. The Perfect core toolset and framework for Swift Developers. (For mobile back-end development, website and API development, and more…)

* [631106979 / WCLShineButton](https://github.com/631106979/WCLShineButton):
        This is a UI lib for iOS. Effects like shining.

* [matteocrippa / awesome-swift](https://github.com/matteocrippa/awesome-swift):
        A collaborative list of awesome swift resources. Feel free to contribute!

* [Moya / Moya](https://github.com/Moya/Moya):
        Network abstraction layer written in Swift.

* [lhc70000 / iina](https://github.com/lhc70000/iina):
        The modern video player for macOS.

#### objective-c
* [airbnb / lottie-ios](https://github.com/airbnb/lottie-ios):
        An iOS library to natively render After Effects vector animations

* [jkpang / PPRows](https://github.com/jkpang/PPRows):
        It Can Calculate how many lines of code you write on Mac ; 在Mac上优雅的计算你写了多少行代码

* [AFNetworking / AFNetworking](https://github.com/AFNetworking/AFNetworking):
        A delightful networking framework for iOS, OS X, watchOS, and tvOS.

* [exponent / exponent](https://github.com/exponent/exponent):
        Exponent iOS/Android Client

* [iphone5solo / Gank](https://github.com/iphone5solo/Gank):

📱一款追求全新用户体验的干货集中营 iOS客户端

* [iosre / WeChatRobotForExample](https://github.com/iosre/WeChatRobotForExample):
        A Powerful WeChat Robot for Example

* [youngsoft / MyLinearLayout](https://github.com/youngsoft/MyLinearLayout):
        MyLayout is a powerful iOS UI framework implemented by Objective-C. It integrates the functions with Android Layout,iOS AutoLayout,SizeClass, HTML CSS float and flexbox and bootstrap. So you can use LinearLayout,RelativeLayout,FrameLayout,TableLayout,FlowLayout,FloatLayout,PathLayout,LayoutSizeClass to build your App 自动布局 UIView UITableView UICo…

* [coolnameismy / BabyBluetooth](https://github.com/coolnameismy/BabyBluetooth):

👶 The easiest way to use Bluetooth (BLE )in ios/os ,even bady can use . 一个非常容易使用的蓝牙库,适用于ios和os

* [brhs / nomouse](https://github.com/brhs/nomouse):
        Discourage use of mouse

* [WenchaoD / FSCalendar](https://github.com/WenchaoD/FSCalendar):
        A fully customizable iOS calendar library, compatible with Objective-C and Swift

* [rs / SDWebImage](https://github.com/rs/SDWebImage):
        Asynchronous image downloader with cache support as a UIImageView category

* [Instagram / IGListKit](https://github.com/Instagram/IGListKit):
        A data-driven UICollectionView framework for building fast and flexible lists.

* [dzenbot / DZNEmptyDataSet](https://github.com/dzenbot/DZNEmptyDataSet):
        A drop-in UITableView/UICollectionView superclass category for showing empty datasets whenever the view has no content to display

* [ChenYilong / iOSInterviewQuestions](https://github.com/ChenYilong/iOSInterviewQuestions):
        iOS面试题集锦(附答案)--学习交流群515295083

* [alibaba / BeeHive](https://github.com/alibaba/BeeHive):

🐝 BeeHive is a solution for iOS Application module programs, it absorbed the Spring Framework API service concept to avoid to coupling between modules.

* [coderyi / DecouplingKit](https://github.com/coderyi/DecouplingKit):
        decoupling between modules in your iOS Project. iOS模块化过程中模块间解耦方案

* [bang590 / JSPatch](https://github.com/bang590/JSPatch):
        JSPatch bridge Objective-C and Javascript using the Objective-C runtime. You can call any Objective-C class and method in JavaScript by just including a small engine. JSPatch is generally used to hotfix iOS App.

* [BradLarson / GPUImage](https://github.com/BradLarson/GPUImage):
        An open source iOS framework for GPU-based image and video processing

* [twitter / ios-twitter-image-pipeline](https://github.com/twitter/ios-twitter-image-pipeline):
        Twitter Image Pipeline is a robust and performant image loading and caching framework for iOS clients

* [Mantle / Mantle](https://github.com/Mantle/Mantle):
        Model framework for Cocoa and Cocoa Touch

* [realm / realm-cocoa](https://github.com/realm/realm-cocoa):
        Realm is a mobile database: a replacement for Core Data & SQLite

* [marcuswestin / WebViewJavascriptBridge](https://github.com/marcuswestin/WebViewJavascriptBridge):
        An iOS/OSX bridge for sending messages between Obj-C and JavaScript in UIWebViews/WebViews

* [ViccAlexander / Chameleon](https://github.com/ViccAlexander/Chameleon):
        Flat Color Framework for iOS (Obj-C & Swift)

* [ivpusic / react-native-image-crop-picker](https://github.com/ivpusic/react-native-image-crop-picker):
        iOS/Android image picker with support for camera, configurable compression, multiple images and cropping

* [snail-z / SnailQuickMaskPopups](https://github.com/snail-z/SnailQuickMaskPopups):
        快速创建蒙版视图,并轻松弹出窗口视图和添加拖动事件。

#### go
* [bcicen / ctop](https://github.com/bcicen/ctop):
        Top-like interface for container metrics

* [knqyf263 / pet](https://github.com/knqyf263/pet):
        Simple command-line snippet manager, written in Go.

* [StackExchange / dnscontrol](https://github.com/StackExchange/dnscontrol):
        Synchronize your DNS to multiple providers from a simple DSL

* [kubernetes / kubernetes](https://github.com/kubernetes/kubernetes):
        Production-Grade Container Scheduling and Management

* [golang / go](https://github.com/golang/go):
        The Go programming language

* [docker / docker](https://github.com/docker/docker):
        Docker - the open-source application container engine

* [future-architect / vuls](https://github.com/future-architect/vuls):
        Vulnerability scanner for Linux/FreeBSD, agentless, written in Go

* [Vladimir37 / Backlun](https://github.com/Vladimir37/Backlun):
        Static API for training your front-end skills

* [astaxie / build-web-application-with-golang](https://github.com/astaxie/build-web-application-with-golang):
        A golang ebook intro how to build a web with golang

* [avelino / awesome-go](https://github.com/avelino/awesome-go):
        A curated list of awesome Go frameworks, libraries and software

* [coreos / clair](https://github.com/coreos/clair):
        Vulnerability Static Analysis for Containers

* [pingcap / tidb](https://github.com/pingcap/tidb):
        TiDB is a distributed NewSQL database compatible with MySQL protocol

* [grafana / grafana](https://github.com/grafana/grafana):
        The tool for beautiful monitoring and metric analytics & dashboards for Graphite, InfluxDB & Prometheus & More

* [gin-gonic / gin](https://github.com/gin-gonic/gin):
        Gin is a HTTP web framework written in Go (Golang). It features a Martini-like API with much better performance -- up to 40 times faster. If you need smashing performance, get yourself some Gin.

* [gogits / gogs](https://github.com/gogits/gogs):
        Gogs is a painless self-hosted Git service.

* [hashicorp / terraform](https://github.com/hashicorp/terraform):
        Terraform is a tool for building, changing, and combining infrastructure safely and efficiently.

* [containous / traefik](https://github.com/containous/traefik):
        Træfɪk, a modern reverse proxy

* [spf13 / hugo](https://github.com/spf13/hugo):
        A Fast and Flexible Static Site Generator built with love in GoLang

* [grpc-ecosystem / grpc-gateway](https://github.com/grpc-ecosystem/grpc-gateway):
        gRPC to JSON proxy generator

* [junegunn / fzf](https://github.com/junegunn/fzf):

🌸 A command-line fuzzy finder written in Go

* [syncthing / syncthing](https://github.com/syncthing/syncthing):
        Open Source Continuous File Synchronization

* [golang / dep](https://github.com/golang/dep):
        Go dependency tool

* [astaxie / beego](https://github.com/astaxie/beego):
        beego is an open-source, high-performance web framework for the Go programming language.

* [prometheus / prometheus](https://github.com/prometheus/prometheus):
        The Prometheus monitoring system and time series database.

* [labstack / echo](https://github.com/labstack/echo):
        High performance, minimalist Go web framework

#### javascript
* [react-community / create-react-native-app](https://github.com/react-community/create-react-native-app):
        Create a React Native app on any OS with no build config.

* [freeCodeCamp / freeCodeCamp](https://github.com/freeCodeCamp/freeCodeCamp):
        The https://freeCodeCamp.com open source codebase and curriculum. Learn to code and help nonprofits.

* [wix / react-native-interactable](https://github.com/wix/react-native-interactable):
        Experimental implementation of high performance interactable views in React Native

* [bailicangdu / vue2-elm](https://github.com/bailicangdu/vue2-elm):
        基于 vue2 + vuex 构建一个具有 45 个页面的大型单页面应用

* [vuejs / vue](https://github.com/vuejs/vue):
        A progressive, incrementally-adoptable JavaScript framework for building UI on the web.

* [facebookincubator / create-react-app](https://github.com/facebookincubator/create-react-app):
        Create React apps with no build configuration.

* [camwest / react-slot-fill](https://github.com/camwest/react-slot-fill):
        Slot & Fill component for merging React subtrees together. Portal on steroids.

* [facebook / react](https://github.com/facebook/react):
        A declarative, efficient, and flexible JavaScript library for building user interfaces.

* [prettier / prettier](https://github.com/prettier/prettier):
        Prettier is an opinionated JavaScript formatter.

* [thejameskyle / react-loadable](https://github.com/thejameskyle/react-loadable):
        A higher order component for loading components with promises.

* [mzabriskie / axios](https://github.com/mzabriskie/axios):
        Promise based HTTP client for the browser and node.js

* [twbs / bootstrap](https://github.com/twbs/bootstrap):
        The most popular HTML, CSS, and JavaScript framework for developing responsive, mobile first projects on the web.

* [getify / You-Dont-Know-JS](https://github.com/getify/You-Dont-Know-JS):
        A book series on JavaScript. @YDKJS on twitter.

* [reswitched / PegaSwitch](https://github.com/reswitched/PegaSwitch):
        PegaSwitch is an exploit toolkit for the Nintendo Switch

* [SunJieMing / js-minicamp-homework-1](https://github.com/SunJieMing/js-minicamp-homework-1):
* [facebookincubator / idx](https://github.com/facebookincubator/idx):
        Library for accessing arbitrarily nested, possibly nullable properties on a JavaScript object.

* [airbnb / javascript](https://github.com/airbnb/javascript):
        JavaScript Style Guide

* [facebook / react-native](https://github.com/facebook/react-native):
        A framework for building native apps with React.

* [storybooks / react-storybook](https://github.com/storybooks/react-storybook):
        UI Component Dev Environment for React

* [yarnpkg / yarn](https://github.com/yarnpkg/yarn):

📦🐈 Fast, reliable, and secure dependency management.

* [d3 / d3](https://github.com/d3/d3):
        Bring data to life with SVG, Canvas and HTML. 📊📈🎉

* [zeit / next.js](https://github.com/zeit/next.js):
        Framework for server-rendered React apps

* [webpack / webpack](https://github.com/webpack/webpack):
        A bundler for javascript and friends. Packs many modules into a few bundled assets. Code Splitting allows to load parts for the application on demand. Through "loaders," modules can be CommonJs, AMD, ES6 modules, CSS, Images, JSON, Coffeescript, LESS, ... and your custom stuff.

* [pinterest / service-workers](https://github.com/pinterest/service-workers):
        A collection of utilities for creating/testing/experimenting with service workers.

* [ReactTraining / react-router](https://github.com/ReactTraining/react-router):
        Declarative routing for React

#### ruby
* [fastlane / fastlane](https://github.com/fastlane/fastlane):

🚀 The easiest way to automate building and releasing your iOS and Android apps

* [rails / rails](https://github.com/rails/rails):
        Ruby on Rails

* [jekyll / jekyll](https://github.com/jekyll/jekyll):

🌐 Jekyll is a blog-aware, static site generator in Ruby

* [HatBashBR / HatCloud](https://github.com/HatBashBR/HatCloud):
        Bypass CloudFlare with Ruby

* [Thibaut / devdocs](https://github.com/Thibaut/devdocs):
        API Documentation Browser

* [Homebrew / brew](https://github.com/Homebrew/brew):

🍺 The missing package manager for macOS

* [rapid7 / metasploit-framework](https://github.com/rapid7/metasploit-framework):
        Metasploit Framework

* [kilimchoi / engineering-blogs](https://github.com/kilimchoi/engineering-blogs):
        A curated list of engineering blogs

* [rails / webpacker](https://github.com/rails/webpacker):
        Use Webpack to manage app-like JavaScript modules in Rails

* [openebs / openebs](https://github.com/openebs/openebs):
        OpenEBS is containerized storage for containers integrated tightly into K8S and other environments and based on distributed block storage and containerization of storage control. OpenEBS derives intent from K8S and other YAML or JSON such as per container QoS SLAs, tiering and replica policies, and more. OpenEBS is EBS API compliant as well.

* [mitchellh / vagrant](https://github.com/mitchellh/vagrant):
        Vagrant is a tool for building and distributing development environments.

* [caskroom / homebrew-cask](https://github.com/caskroom/homebrew-cask):
        A CLI workflow for the administration of Mac applications distributed as binaries

* [lyptt / struct](https://github.com/lyptt/struct):
        Xcode projects on steroids

* [elastic / logstash](https://github.com/elastic/logstash):
        Logstash - transport and process your logs, events, or other data

* [sass / sass](https://github.com/sass/sass):
        Sass makes CSS fun again.

* [discourse / discourse](https://github.com/discourse/discourse):
        A platform for community discussion. Free, open, simple.

* [Tim9Liu9 / TimLiu-iOS](https://github.com/Tim9Liu9/TimLiu-iOS):
        iOS开发常用三方库、插件、知名博客等等

* [ruby / ruby](https://github.com/ruby/ruby):
        The Ruby Programming Language

* [realm / jazzy](https://github.com/realm/jazzy):
        Soulful docs for Swift & Objective-C

* [plataformatec / simple_form](https://github.com/plataformatec/simple_form):
        Forms made easy for Rails! It's tied to a simple DSL, with no opinion on markup.

* [tmuxinator / tmuxinator](https://github.com/tmuxinator/tmuxinator):
        Manage complex tmux sessions easily

* [bbatsov / rubocop](https://github.com/bbatsov/rubocop):
        A Ruby static code analyzer, based on the community Ruby style guide.

* [resque / resque](https://github.com/resque/resque):
        Resque is a Redis-backed Ruby library for creating background jobs, placing them on multiple queues, and processing them later.

* [mperham / sidekiq](https://github.com/mperham/sidekiq):
        Simple, efficient background processing for Ruby

* [shiroyasha / kamisama](https://github.com/shiroyasha/kamisama):
        Start, monitor, and observe background worker processes, from Ruby.


text:
import React, { Component } from 'react'
import CardList from './List/CardList'
import SearchBar from './Search/SearchBar'
import ContextMenu from './List/contextmenu'
import TransparentOverlay from './transparentOverlay'
import EditForm from './Overlay/EditForm'
import Overlay from './Overlay/Overlay'
import Folder from './Overlay/Folder'
import LeftMenu from './Menu/LeftMenu'
import * as ninja from './Libraries/arrayNinja'

export default class App extends Component {
    constructor(props) {
        super(props)
        try {
            var json = JSON.parse(localStorage["data"])
            var data = json.data
            var localDate = json.date
        } catch(err) {
            data = err
        }

        try {
            var trash = JSON.parse(localStorage["trash"])
        } catch(err) {
            trash = []
        }
        this.state = {  initialData: data,
                        localDate: localDate,
                        data: [],
                        trash: trash,
                        filterList: true,
                        cardDragStart: '',
                        contextMenu: false,
                        contextTop: '',
                        contextLeft: '',
                        context: '',
                        showEdit: false,
                        showTrash: false,
                        showFolder: false,
                        activeFolder: [],
                        topFolder: ''}
    }

    loadData() {

        fetch(this.props.url, {
            method: 'get'
        }).then(function(res) {
            return res.json()
        }).then(function(json){
            if (json.date < this.state.localDate ){
                this.saveData(this.state.initialData, this.state.localDate)
            } else if (json.date == this.state.localDate){
            } else {
                 this.setState({data: json.data,
                                initialData: json.data,
                                localDate: json.date})
                 this.saveData(json.data, json.date)
             }
        }).catch(function(err) {})
    }

    saveData(data, date){
        var newData = { date: date,
                        data: data}
        localStorage["data"] = JSON.stringify(newData)
        fetch(this.props.url, {
          headers: new Headers({
              'Content-Type': 'application/json'
          }),
          method: 'post',
          body: JSON.stringify(newData)
        })

   }

    componentDidMount(){
        this.setState({data: this.state.initialData})
        this.loadData()
        var date = new Date()
    }

    expandURL(url){
        var item = {    name:  url.url.replace('http://','').replace('https://','').replace('www.','').split(/[/?#]/)[0],
                        url: ((!/^https?:\/\//i.test(url.url)) ? 'http://' + url.url : url.url),
                        description:"Tady bude meta description", img:""}
        return item
    }

    pageClick(){
        this.setState({contextMenu: false})
    }

    cardRightClick(evt, context){
        this.setState({contextMenu: true,
                       contextTop: evt.clientY + window.scrollY ,
                       contextLeft: evt.clientX,
                       context: context })
    }

    hideOverlay(){
        this.setState({ showEdit: false,
                        showTrash: false,
                        showFolder: false})
    }

    showAdd(updatedList){
        if (updatedList.length == this.state.initialData.length){
            this.state.filterList = true
        } else {
            this.state.filterList = false
        }
    }

    addItem(url){
        var item = this.expandURL(url)
        var newData = ninja.add(this.state.data, item)
        this.setState({ initialData : newData,
                        data: newData })
        this.saveData(newData, Date.now())
    }
    addFolder(){
        var item = {type: "folder",
                    name:"Moje složka",
                    data: []
                   }
        var newData = ninja.add(this.state.data, item)
        this.setState({ initialData : newData,
                        data: newData })
        this.saveData(newData, Date.now())
    }
    addTrashFolder(){
        var item = {type: "folder",
                    name:"Moje složka",
                    data: []}
        var newTrash = ninja.add(this.state.trash, item)
        this.setState({ trash : newTrash})
        localStorage["trash"] = JSON.stringify(newTrash)
    }
    openFolder(topFolder, id){
        if (topFolder == "trash"){
            this.setState({ activeFolder: this.state.trash[id],
                            showFolder: true,
                            contextMenu: false,
                            showTrash: false})
        }else{
            this.setState({ activeFolder: this.state.data[id],
                            showFolder: true,
                            contextMenu: false})
        }
        window.scrollTo(0,0)
        this.state.topFolder = topFolder
    }
    editItem(url) {
        var item = this.expandURL(url)
        item.id = this.state.context.id
        var newData = ninja.edit(this.state.data, item)
        this.setState({ initialData : newData,
                        data: newData })
        this.saveData(newData, Date.now())
    }
    changeName(name){
        var item = this.state.activeFolder
        item.name = name
        var newData = ninja.edit(this.state.data, item)
        this.setState({ initialData : newData,
                        data: newData })
        this.saveData(newData, Date.now())
    }
    moveToTrash(id) {
        if (this.state.showFolder){
            var newTrash = ninja.moveToArray(this.state.data[this.state.activeFolder.id].data, this.state.trash, id)
            var newData = this.state.data
            newData[this.state.activeFolder.id].data = newTrash.source
            this.setState({ data: newData,
                            trash: newTrash.target})
            localStorage["trash"].data = JSON.stringify(newTrash.target)
            this.saveData(newData, Date.now())
        }else{
             var newData = ninja.moveToArray(this.state.data, this.state.trash, id)
             this.setState({ data: newData.source,
                        trash: newData.target})
            localStorage["trash"] = JSON.stringify(newData.target)
            this.saveData(newData.source, Date.now())
        }
    }
    moveFromTrash(id) {
        if (this.state.showFolder){
            var newData = ninja.moveToArray(this.state.trash[this.state.activeFolder.id].data, this.state.data, id)
            var newTrash = this.state.trash
            newTrash[this.state.activeFolder.id].data = newData.source
            this.setState({ trash: newTrash,
                            data: newData.target})
            localStorage["trash"] = JSON.stringify(newTrash)
            this.saveData(newData.target, Date.now())
        }else{
            var newData = ninja.moveToArray(this.state.trash, this.state.data, id)
            this.setState({ trash: newData.source,
                            data: newData.target})
            localStorage["trash"] = JSON.stringify(newData.source)
            this.saveData(newData.target, Date.now())
        }
    }
    removeItem(id) {
        var newData = ninja.remove(this.state.trash, id)
        this.setState({ trash: newData})
        localStorage["trash"].data = JSON.stringify(newData)
    }
    cardDragOver(id){
        var newData = ninja.move(this.state.data, this.state.cardDragStart, id)
        this.setState({cardDragStart: id,
                       initialData: newData,
                       data: newData})
    }
    trashDragOver(id){
        var newTrash= ninja.move(this.state.trash, this.state.cardDragStart, id)
        this.setState({cardDragStart: id,
                       trash: newTrash})
    }
    folderDragOver(id){
        if (this.state.topFolder == "trash"){
            var newTrash = this.state.trash
            newTrash[this.state.activeFolder.id].data = ninja.move(this.state.trash[this.state.activeFolder.id].data, this.state.cardDragStart, id)
            this.setState({ cardDragStart: id,
                            trash: newTrash})
        }else{
            var newData = this.state.data
            newData[this.state.activeFolder.id].data = ninja.move(this.state.data[this.state.activeFolder.id].data, this.state.cardDragStart, id)
            this.setState({ cardDragStart: id,
                            data: newData})
        }
    }

    cardDragStart(id){
        this.setState({cardDragStart: id})
    }
    dropCard(){
        localStorage["trash"] = JSON.stringify(this.state.trash)
        this.saveData(this.state.initialData, Date.now())
    }

    handleContextDelete(){
        this.removeItem(this.state.data[this.state.context.id])
        this.setState({contextMenu: false})
    }
    handleContextEdit(){
        this.setState({showEdit: true,
                       contextMenu: false})
    }
    handleContextOpen(){
        if (this.state.context.type == "folder"){
            for (let i = 0; i < this.state.context.data.length; i++) {
                window.open(this.state.context.data[i].url)
            }
        }else{
            window.open(this.state.context.url)
        }
        this.setState({contextMenu: false})
    }
    handleTrash(){
        this.setState({ showTrash: true,
                        contextMenu: false,
                        topFolder: "trash"
                      })
    }
    moveToFolder(card){
            var id = card.id
            if (this.state.topFolder == "trash"){
                if (card.type == "folder"){
                    var newTrash = ninja.moveToArray(this.state.trash, this.state.trash[id].data, this.state.cardDragStart)
                    this.setState({trash: newTrash.source})
                    localStorage["trash"] = JSON.stringify(newTrash.source)
                }else{
                    this.addTrashFolder()
                    var newWorld = ninja.moveToArray(this.state.trash, this.state.trash[this.state.trash.length-1].data, this.state.cardDragStart)
                    var newTrash = ninja.moveToArray(newWorld.source, newWorld.source[newWorld.source.length-1].data, id)
                    newTrash.source = ninja.move(newTrash.source, newTrash.source.length-1, id)
                    this.setState({trash: newTrash.source})
                    localStorage["trash"] = JSON.stringify(newTrash.source)
                }
            }else{
                if (card.type == "folder"){
                    var newData = ninja.moveToArray(this.state.data, this.state.data[id].data, this.state.cardDragStart)
                    this.setState({data: newData.source})
                    this.saveData(newData.source, Date.now())
                }else{
                    this.addFolder()
                    var newWorld = ninja.moveToArray(this.state.data, this.state.data[this.state.data.length-1].data, this.state.cardDragStart)
                    var newData = ninja.moveToArray(newWorld.source, newWorld.source[newWorld.source.length-1].data, id)
                    newData.source = ninja.move(newData.source, newData.source.length-1, id)
                    this.setState({data: newData.source})
                    this.saveData(newData.source, Date.now())
                }
            }
    }

    moveFromFolder(){
        if (this.state.topFolder == "trash"){
        }else{
            var newWorld = ninja.moveToArray(this.state.data[this.state.activeFolder.id].data, this.state.data, this.state.cardDragStart)
            var newData = newWorld.target
            newData[this.state.activeFolder.id].data = newWorld.source
            this.setState({data: newData})
            this.saveData(newData, Date.now())
        }
    }

    filterList(evt){
        var updatedList
        updatedList = this.state.initialData.filter(function(item){
            return item.name.toLowerCase().search(
            evt.value.toLowerCase()) !== -1
        })
        this.setState({data: updatedList})
        this.showAdd(updatedList)
    }

    render() {
        var contextMenu
        var contextStyle = {top: this.state.contextTop,
                            left: this.state.contextLeft}
        if (this.state.contextMenu){
            contextMenu = (

                    <ContextMenu    style = {contextStyle}
                                    onDelete = {this.handleContextDelete.bind(this)}
                                    onEdit = {this.handleContextEdit.bind(this)}
                                    onOpen = {this.handleContextOpen.bind(this)}/>
                    <TransparentOverlay onClick={this.pageClick.bind(this)}/>

            )
        }

        var editOverlay
        var id = this.state.context.id - 1
        if (this.state.showEdit){
            editOverlay = (

                    <EditForm    onSubmit={this.editItem.bind(this)}
                                 onHide={this.hideOverlay.bind(this)}
                                 url={this.state.context.url}/>
                    <Overlay     onClick={this.hideOverlay.bind(this)}/>

                )
        }

        var trashOverlay
        if (this.state.showTrash){
            trashOverlay = (

                    <Folder name = "Koš"
                            onHide={this.hideOverlay.bind(this)}
                            data = {this.state.trash}
                            onDelete = {this.removeItem.bind(this)}
                            cardDragOver = {this.trashDragOver.bind(this)}
                            cardDragStart = {this.cardDragStart.bind(this)}
                            dropCard = {this.dropCard.bind(this)}
                            onUndelete = {this.moveFromTrash.bind(this)}
                            cardRightClick = {function(){}}
                            canDelete = {true}
                            openFolder = {this.openFolder.bind(this, "trash")}
                            moveToFolder ={this.moveToFolder.bind(this)}
                            changeName ={function(){}}
                            moveFromFolder = {this.moveFromFolder.bind(this)}
                            closeOverlay = {this.hideOverlay.bind(this)}/>
                    <Overlay  onClick={this.hideOverlay.bind(this)}/>

                )
        }

        var folderOverlay
        if (this.state.showFolder){
            folderOverlay = (

                    <Folder name = {this.state.activeFolder.name}
                            showAdd = {(this.state.topFolder == "trash" ? false : true)}
                            onAdd = {this.addItem.bind(this)}
                            onHide={this.hideOverlay.bind(this)}
                            data = {this.state.activeFolder}
                            onDelete = {this.moveToTrash.bind(this)}
                            cardDragOver = {this.folderDragOver.bind(this)}
                            cardDragStart = {this.cardDragStart.bind(this)}
                            dropCard = {this.dropCard.bind(this)}
                            onUndelete = {this.moveFromTrash.bind(this)}
                            cardRightClick = {this.cardRightClick.bind(this)}
                            canDelete = {(this.state.topFolder == "trash" ? true : false)}
                            openFolder = {this.openFolder.bind(this, "folder")}
                            moveToFolder ={function(){}}
                            changeName ={this.changeName.bind(this)}
                            moveFromFolder = {this.moveFromFolder.bind(this)}
                            closeOverlay = {this.hideOverlay.bind(this)}/>
                    <Overlay  onClick={this.hideOverlay.bind(this)}/>

                )
        }

        return (

                <SearchBar  onFilter = {this.filterList.bind(this)}/>
                <CardList   data = {this.state.data}
                            onDelete = {this.moveToTrash.bind(this)}
                            showAdd = {this.state.filterList}
                            onAdd = {this.addItem.bind(this)}
                            cardDragOver = {this.cardDragOver.bind(this)}
                            cardDragStart = {this.cardDragStart.bind(this)}
                            cardRightClick = {this.cardRightClick.bind(this)}
                            dropCard = {this.dropCard.bind(this)}
                            openFolder = {this.openFolder.bind(this, "topFolder")}
                            canDelete = {false}
                            moveToFolder ={this.moveToFolder.bind(this)}/>
                <LeftMenu   showTrash = {this.handleTrash.bind(this)}
                            addFolder = {this.addFolder.bind(this)}
                            onDrop ={this.moveToTrash.bind(this, this.state.cardDragStart)}/>
                {contextMenu}
                {editOverlay}
                {trashOverlay}
                {folderOverlay}

        )
    }
}


text:
<?php

class Images extends CI_Model {

    // constructor
    function __construct() {
        parent::__construct();
    }

    // return all images, descending order by post date
    function all() {
        $this->db->order_by("id", "desc");
        $query = $this->db->get('images');
        return $query->result_array();
    }

    // return just the 3 newest images
    function newest() {
        $this->db->order_by("id", "desc");
        $this->db->limit(3);
        $query = $this->db->get('images');
        return $query->result_array();
    }
}