Simple morphing button concept I have been making a jQuery morphing button type thing.

jsFiddle

$(document).ready(function() {

    var morphObject = {

        button: $('button.morphButton'),
        container: $('div.morphContainer'),
        overlay: $('div.overlay'),
        content: $('h1, p'),

        init: function() {
            this.buttonPress();
        },

        buttonPress: function() {
            var mO = morphObject;

            this.button.on('click', function() {

                mO.button.fadeOut(200);
                setTimeout(mO.containerMove, 200);

                });

        },

        containerMove: function() {
            var mO = morphObject;
                span = $('span.close');

            mO.overlay.fadeIn();

            mO.container.animate({
                top: 300,
                left: '50%',
                width: 600,
                height: 400,
                marginLeft: -300 },
                400, function() {
                    mO.content.fadeIn();
                    span.fadeIn();
                    mO.close();
            });

        },

        close: function() {
            var mO = morphObject;

            if ( mO.container.find('span.close').length ) return;

            $('X').appendTo(mO.container);

            var span = $('span.close');

            mO.overlay.add(span).on('click', function() {
                mO.content.fadeOut();
                span.fadeOut();
                mO.overlay.fadeOut();
                setTimeout(mO.animateBack, 200);
            });

        },

        animateBack: function() {
            var mO = morphObject;

            mO.container.animate({
                top: 150,
                left: '20%',
                width: 200,
                height: 70,
                marginLeft: -100 },
                400, function() {
                    mO.button.fadeIn(300);
            });

        }

    }

    morphObject.init();

});

Basically, I would love to have any feedback on what I can improve. Please tell me if I have used any bad coding practises or methods which will cause slow performance.

Looks pretty clean for me.

the only improvement i suggest, is avoid the animateBack harcode position. Maybe you can save before the animation start, and then, back to the last position.

    containerMove: function() {
        var mO = morphObject;
        mO.overlay.fadeIn();

        startPosition = {
            top: morphObject.top(),
            left: morphObject.left(),
            width: morphObject.width(),
            height: morphObject.height(),
            marginLeft: ....
        };

        mO.container.animate({
            top: 300,
            left: '50%',
            width: 600,
            height: 400,
            marginLeft: -300
        },
        400,
        function() {
                mO.content.fadeIn();
                span.fadeIn();
                mO.close();
        });

    },
    animateBack: function() {
        var mO = morphObject;
        mO.container.animate(startPosition);
    }

Also, for my suggestion is a little anoying using a literal object { }, you can use a constructor new function () {} is better for set properties and  handle scopes.


ViewModel creator design I am trying to move some logic outside my controller for creating my view models for the view.

I have a lot of queries to fire to make sure the complete view model is ready, so to keep my controllers thin I came up with this design - any feedback, comments or suggestions are welcome!

The interfaces looks as following:

public interface IViewModel
{
    // Marker interface
}

public interface IHandleViewModel<TViewModel> where TViewModel : IViewModel
{
    Task<TViewModel> Handle();
}

public interface IProcessViewModels
{
    Task<TViewModel> Create<TViewModel>() where TViewModel : IViewModel, new();
}

The implementation of the IProcessViewModels is using dependency injection (Simple Injector) to find the correct handler for the view model:

internal sealed class ViewModelProcessor : IProcessViewModels
{
    private readonly Container _container;

    public ViewModelProcessor(Container container)
    {
        _container = container;
    }

    public Task<TViewModel> Create<TViewModel>() where TViewModel : IViewModel, new()
    {
        var handlerType = typeof(IHandleViewModel<>).MakeGenericType(typeof(TViewModel));
        dynamic handler = _container.GetInstance(handlerType);
        return handler.Handle();
    }
}

The setup is done in the Composition Root using this extension:

 public static void RegisterViewModels(this Container container, Assembly[] viewModelAssemblies)
    {
        if (container == null)
            throw new ArgumentNullException("container");

        if (viewModelAssemblies == null)
            throw new ArgumentNullException("viewModelAssemblies");

        container.RegisterSingle<IProcessViewModels, ViewModelProcessor>();
        container.RegisterManyForOpenGeneric(typeof(IHandleViewModel<>), viewModelAssemblies);

        container.RegisterSingleDecorator(
            typeof(IHandleViewModel<>),
            typeof(ViewModelLifetimeScopeDecorator<>)
        );
    }

The usage

public HomeViewModel : IViewModel {

    public string PageTitle { get; set; }

    public IList<TodoItem> TodoItems { get; set; }

}

public HandleHomeViewModel : IHandleViewModel<HomeViewModel> {

    private readonly IProcessQueries _queries; // CQRS

    public HandleHomeViewModel(IProcessQueries queries) {
            _queries = queries;
    }

    public async Task<HomeViewModel> Handle() {
        var model = new HomeViewModel {
             PageTitle = "This is the page title";
             TodoItems = await _queries.Execute(new GetTodoItemsFromDatabase()); // for demo purpose
        }

        return model;
    }

}

public class HomeController : Controller
{
    private readonly IProcessViewModels _models;

    public HomeController(IProcessViewModels models)
    {
        _models = models;
    }

    public async Task<ActionResult> Index()
    {
            var model = await _models.Create<HomeViewModel>();
            return model;
    }
}

I would say your design is fine. In general I advise injecting IHandleViewModel<T> implementations directly into consumers, because this makes it easier to verify the object graph directly and makes it clearer what the consumer actually depends on. You should do this, unless you regularly inject multiple IHandleViewModel<T>s into the same consumer, while you are pretty sure that these consumers are NOT violating the Single Responsibility Principle.

But if you decide to inject the ViewModelProcessor mediator, I advice to add a single unit/integration test to the system that verifies whether there is an IHandleViewModel<T> implementation for each view model in the system. This prevents you from getting an exception at runtime because an implementation is missing.

The IViewModel marker interface gets useful when writing such unit test, because this interface allows you to find all view models easily as follows:

// Arrange
var viewModelHandlerTypes =
    from assembly in AppDomain.CurrentDomain.GetAssemblies()
    from type in assembly.GetTypes()
    where typeof(IViewModel).IsAssignableFrom(type)
    where !type.IsAbstract && !type.IsGenericTypeDefinition
    select typeof(IHandleViewModel<>).MakeGenericType(type);

// Act
viewModelHandlerTypes.ToList().ForEach(container.GetInstance);

About Async Programming

A last suggestion I would like to make is to get rid of the complete async programming model. Asynchronous methods tend to spread through your application like a virus and make both programming and debugging your application much harder. Yes, this asynchronous programming has become WAY easier than it used to be, but it is STILL harder than synchronous programming and it will probably stay harder untill the .NET runtime has been rewritten from the ground up (if that's even possible).

I know this is against popular opinion, but there is hardly ever a reason to polute your entire code base with this asynchronous programming model. Main reason for Microsoft to push this programming model really hard is because it is more efficient when running in the cloud. This makes sense, because in Azure, you pay per CPU cycle and per the number of machines you need. But on the other hand, asynchronous programming costs way more developer cycles, and because developers are quite expensive, it is quite unlikely that your savings on the Azure bills will actually compensate the extra developer costs. But obviously, you will have to do the math yourself.

Don't get me wrong, of course we want -and need- responsive UIs so we might need a few async/await calls inside your Window, Page or View Model classes in our presentation layer built with WPF, Silverlight, Win Forms or some other client technology. You can still do this, even though your whole code base below is synchronous, with synchonous calls to the database, web services and the file system. When doing that, you will still be able to make your UI responsive, but the only difference is that you'll have a background thread sleeping most of the time, instead of using I/O completion ports. But I've never ever worked on an application where having this single extra background thread was a problem. Even for Windows Phone applications this is a non-issue.

But since you're building an MVC application, don't bother in making your controller code asynchronous, the user's browser will wait anyway, even if you make your controller asynchronous.

Asynchronous programming may be the new shiny thing in the .NET world, and with some training and experience, we can become quite effective as developers in applying it, but even than it is more painful than synchronous progamming (which is hard enough by itself), and instead of spending money on training developers learning how to do async, I rather spend this money in training them to learn the SOLID design principles, Test Driven Development, Functional Programming or writing clean code. There are so many other skills that are probably more important and more effective in reducing the total cost of ownership, that I rather have my money on that first.


Optimisation suggestions for Min Heap Can someone please optimisations for my implementation of min heap.Its performance is poor compared to the heapq module in python. My use case for the MinHeap class is 'N' insertions and extractions where N > 1,000,000.

import math

class MinHeap:
    def __init__(self, arr):
        self.__items = arr
        self.build_heap()

    def size(self):
        """
        Returns the size of heap.
        """
        return len(self.__items)

    def items(self):
        """
        Returns the elements in the heap.
        """
        return self.__items

    def build_heap(self):
        """
        Builds heap using the list of elements.
        """
        for node_number in xrange(int(len(self.__items) / 2), 0, -1):
            self.heapify(node_number)

    def heapify(self, node_number):
        """
        Ensure that node follows heap property.
        """
        # return if leave node
        if node_number > int(len(self.__items) / 2):
            return

        node = self.__items[node_number-1]
        left_child = self.__items[(2 * node_number)-1] if (((2 * node_number)-1) < len(self.__items)) else None

        right_child = self.__items[(2 * node_number + 1)-1] if (((2 * node_number + 1)-1) < len(self.__items)) else None

        min_node = node

        if left_child != None and right_child != None:
            min_node = min(node, left_child, right_child)
        elif left_child != None :
            min_node = min(node, left_child)
        elif right_child != None :
            min_node = min(node, right_child)

        if min_node == node:
            return
        elif left_child!=None and min_node == left_child:

            self.__items[node_number - 1], self.__items[(2 * node_number)-1] = self.__items[(2 * node_number)-1], self.__items[node_number - 1]
            self.heapify(2 * node_number)
        elif right_child!=None and min_node == right_child:

            self.__items[node_number - 1], self.__items[(2 * node_number + 1)-1] = self.__items[(2 * node_number + 1)-1], self.__items[node_number - 1]
            self.heapify(2 * node_number + 1)

    def extract_min(self):
        """
        Returns the minimum element.
        """
        length = len(self.__items)
        if length == 0:
            return
        self.__items[0], self.__items[length-1] = self.__items[length-1], self.__items[0]
        min_element =  self.__items.pop()
        self.heapify(1);
        return min_element

    def insert(self, num):
        """
        Inserts a new element in the heap.
        """
        self.__items.append(num)
        current_node = len(self.__items)
        parent_node = int(current_node / 2)
        while current_node > 1:
            min_node = min(self.__items[current_node-1], self.__items[parent_node-1])
            if min_node == self.__items[parent_node-1]:
                break
            self.__items[current_node-1], self.__items[parent_node-1] = self.__items[parent_node-1], self.__items[current_node-1]
            current_node = parent_node
            parent_node = int(current_node / 2)

Docstrings with every method: Way to go, document MinHeap, too and revisit PEP257 for details.
Your MinHeap tackles the same task as module heapq using much the same strategy, and you express dissatisfaction with run time - a side by side comparison:

in many places, you compute something like node_number or parent_node to repeatedly index __items - not using that variable, but something like parent_node - 1
(Code the way you think about it: if there are multiple places where you want the element at index i-1, write a[i-1], b[i-1]…, if you think same index happening to be i-1, write j = i-1, a[j], b[j]…)
(An optimising compiler can be expected to eliminate common sub-expressions; that is more difficult for an interpreter to justify (and I seem to remember why it might be hard for python).)
insert() assigns the new value to every candidate position, while heapq._siftdown()/heappush() just moves down items of lesser priority and puts the new item once/twice.
extract_min() swaps the items at both ends instead of just replacing the top priority item (see heapq.heappop())
heapify() conditionally assigns None to left_child and right_child to go on and conditionally execute statements comparing these values to None (using != instead of is&not)

heapq._siftup() uses the realistic approach of not comparing values "on the way down": half the nodes are leaf nodes, it is much more likely that the new item will end up near the bottom of the heap.

heapq introduces heappushpop() as a "Fast version of a heappush followed by a heappop."

You seem to be bent on thinking of nodes numbered starting from 1 - nothing wrong with that, and coding the way you think about a solution/problem is the only sane way to start:
for a shot from the hip, allocate one more array element, don't use index 0 and drop all of "the - 1".


Start the Python interpreter with an import statement When I'm writing Python, I wanna be able to pull up an interpreter quickly, and import what I need easily. Previously, this involved:

bash4.3 $ python3
>>> import os, sys, readline, lxml

I was irked by the amount of typing in that, so I wrote a couple of tiny bash functions that live in my .bashrc, so I can just do (for example):

bash4.3 $ from os import path
>>>
from os import path: success!
>>>

or:

bash4.3 $ import os, sys, readline, lxml
>>>
imported os, sys, readline, lxml
>>>

Perhaps this might be regarded by some as a non-issue, but for my workflow when I just want to test something quickly, it's really handy.

The functions:

function import () {
  ARGS=$*
  ARGS=$(python3 -c "import re;print(', '.join(re.findall(r'([\d\w]+)[, ]*', '$ARGS')))")
  echo -ne '\0x04' | python3 -i
  python3 -c "import $ARGS" &> /dev/null
  if [[ $? != 0 ]]; then
    echo "sorry, junk module in list"
  else
    echo "imported $ARGS"
  fi
  python3 -i -c "import $ARGS"
}

function from () {
  ARGS=$*
  ARGS=$(python3 -c "import re; s = '$ARGS'.replace(',', ', '); args = ' '.join(re.findall(r'^([\d\w]+) import ([\d\w, ]+)$', s)[0]).split(' '); print('from', args[0], 'import', ' '.join(args[1:]).replace(',', '').replace(' ', ','))")
  echo -ne '\0x04' | python3 -i
  python3 -c "$ARGS" &> /dev/null
  if [[ $? != 0 ]]; then
    echo "junk module in list"
  else
    echo "$ARGS: success!"
  fi
  python3 -i -c "$ARGS"
}

I'm aware python3 -m module exists, however, it requires module to be a fully-fledged module, with an __init__.py and stuff.  If I'm writing a small script that isn't really designed to be a module (i.e, that has module-level expressions that will be run if the module is imported), then I want to import it normally, not with -m.

I'm also aware I could use && and || for short-circuiting over if [[ ]]; then; fi but I consider this more readable.

I'm looking for responses about any part, really, but I'd be most interested to hear whether I'm unknowingly abusing bash in some way (quoting, perhaps?) and about the probable inefficiency of the Python code I'm using to process / sanitise the args.

I'd also be happy to hear about corner cases not caught by my regex / parsing job, and how they sudo rm -rf --no-preserve-root /-ed you.

Use an alias: alias 3=python3.
Use the PYTHONSTARTUP environment variable: import your most frequently used modules whether or not they'd actually get used in this session.
If you insist on doing it your way, I've made an "improvement" over your code:

function import
{
    python3 -ic "import $*"
}

function from
{
    args=$*
    python3 -ic "from ${args%import*} import ${args#*import}"
}

This doesn't guard against anything, nor should you need to.

Hardened (?) version (use at your own risk):

function import
{
    args=$(awk -v q="'" -v ORS=',' '{for(i=1; i<=NF; i++) print q $i q}' <<< "${@/,/ }")
    python3 -ic "for module in eval("\""[$args]"\""): exec(f'import {module}')"
}

Feel free to use any combination of the 3 methods.


Partial ArrayList implementation in C++ I've implemented a C++ version of Java ArrayList. It doesn't contain all the functionalities such as AddAll and iterators and such, but I still would appreciate criticism to my code correctness, style and flow and such

template <typename T>
class ArrayList
{
    public:
        ArrayList();
        ~ArrayList();
        ArrayList(const ArrayList<T>& other);
        ArrayList<T>& operator= (const ArrayList<T>& other);

        void add(const T& item); //Add item to end of array
        void add(const T& item, int index);
        void remove(const T& item); //Remove ALL occurrences of item from array
        void remove(int index);

        T& get(int index);
        void set(int index, const T& item);

    private:
        T* arr;
        int allocatedSize;
        int actualSize;
        const int INCREMENT = 10;
};

template<typename T>
ArrayList<T>::ArrayList()
{
    arr = new T[INCREMENT];
    allocatedSize = INCREMENT;
    actualSize = 0;
}

template<typename T>
ArrayList<T>::~ArrayList()
{
    delete[] arr;
}

template<typename T>
ArrayList<T>::ArrayList(const ArrayList<T>& other)
{
    arr = new T[other.allocatedSize];
    allocatedSize = other.allocatedSize;
    actualSize = other.actualSize;

    for(int i = 0; i<other.actualSize; i++)
        arr[i] = other.arr[i];
}

template<typename T>
ArrayList<T>& ArrayList<T>::operator= (const ArrayList<T>& other)
{
    delete[] arr;
    arr = new T[other.allocatedSize];
    allocatedSize = other.allocatedSize;
    actualSize = other.actualSize;

    for(int i = 0; i<other.actualSize; i++)
        arr[i] = other.arr[i];

    return *this;
}

//Add item to end of array
template<typename T>
void ArrayList<T>::add(const T& item)
{
    if(actualSize < allocatedSize)
    {
        actualSize++;
        arr[actualSize-1] = item;
    }
    else
    {
        allocatedSize += INCREMENT;

        T*cpy = new T[allocatedSize];

        for(int i=0; i<actualSize; i++)
            cpy[i] = arr[i];

        actualSize++;
        cpy[actualSize-1] = item;

        delete[] arr;
        arr = cpy;
    }
}

template<typename T>
void ArrayList<T>::add(const T& item, int index)
{
    if(index < actualSize - 1)
        return;

    allocatedSize = (actualSize < allocatedSize) ? allocatedSize : allocatedSize + INCREMENT;

    T*cpy = new T[allocatedSize];

    for(int i=0; i<index; i++)
        cpy[i] = arr[i];

    cpy[index] = item;

    for(int i = index; i<actualSize; i++)
        cpy[i+1] = arr[i];

    actualSize++;
    delete[] arr;
    arr = cpy;
}

//Remove ALL occurrences of item from array
template<typename T>
void ArrayList<T>::remove(const T& item)
{
    if(actualSize <= 0)
        return;

    T*cpy = new T[allocatedSize];
    T*cpyStart = cpy;
    T*arrStart = arr;

    for(int i = 0; i<actualSize; i++)
    {
        if(arr[i] != item)
        {
            *cpy = *arr;
            cpy++;
        }

        arr++;
    }

    cpy = cpyStart;
    arr = arrStart;

    delete[] arr;
    arr = cpy;
}

template<typename T>
void ArrayList<T>::remove(int index)
{
    if(index > actualSize - 1)
        return;

    for(int i = 0; i<actualSize; i++)
    {
        if(i>=index)
            arr[i] = arr[i+1];
    }
}

template<typename T>
T& ArrayList<T>::get(int index)
{
    return arr[index];
}

template<typename T>
void ArrayList<T>::set(int index, const T& item)
{
    if(index > actualSize - 1)
        return;

    arr[index] = item;
}

Preface

A lot of C++ programmers hate Java being translated to C++. The reason is not that either C++ or Java is bad, the reason is that they are very opposite in nature. Java doesn't make the programmer to worry about performance, safety and memory allocations. It does checks to ensure safety. C++ almost always offers more degree of control, thus providing opportunities to write more optimized code. Despite that, C++ has encapsulation as well, but it keeps it to the minimum required. It provides encapsulation only when it's free (e.g. compile time, or no overhead). It always tries to hold following:

  don't pay for what you don't use

Your code

There are several problems in your code.

You got very close to strong exception safety. Unfortunately, you've missed one step of the copy and swap idiom. Namely the swap, so even if the deletion throws nothing bad will happen to the data in the container. I recommend this answer to enrich knowledge about C++ exception safety.

Your container new[]s everything. So there is a problem: if T is not default constructible, it is not possible to use your container. Some people use term lazy construction, but I prefer the following wording: you should not construct on memory allocation, you should retrieve uninitialized buffer, and then construct when needed. To do so you will need to call operator new. Use only non array version, since array version has very limited use.

Container copies every element on adding. C++11 introduced template parameter packs. With the feature you can write following code:

template <typename T>
template <typename ... Args>
void ArrayList<T>::emplace(Args... args)
{
    if (allocatedSize == actualSize)
    {
        //handle buffer resizing
    }

    new (copy + actualSize) T(args...);
    std::swap(copy, arr);
    ++actualSize;
    delete[] copy;
}

It will handle any constructor that T might have. It will also issue compile time error if there is no matching constructor to input arguments. The new expression used is called placement new. It constructs an element in the pointed to location specified.

Your container grows linearly. Often this is not enough. It will resize buffer too many times which will incur great overhead. Usual implementations increase the size exponentially. VC++ uses growth factor of 2 (probably as many others). Despite that, the article mentions how to get optimal growth factor.

You don't overload operator[]. I know that the code is meant to be ArrayList, but honestly it is not C++ without operator overloading. On top of that, usually container manipulations are not checked in C++. Your remove operation just does nothing on getting out of bounds. Surprising. std::vector uses special at function to perform checks and throw.

C++ programmers use std::size_t defined in <cstddef> when referring to the size of the objects in memory. The type has specific semantic: it is big enough to index all the elements in the memory. It is unsigned, which will suggest that users shouldn't pass negative values to the add and remove.

What is missing

Aside from iterators, here is the list:

std::initalizer_list constructor
resize
reserve
range add (based on iterators)
range erase (based on iterators)
Non resizing add on position (add should be amortized constant)
Move constructor
Move assignment operator (with point 7 and your already written code, you will fully implement rule of 5).
Placement add (using placement new)
Moving add
iterator based remove, add, constructor.
Move elements on buffer resizing if move is noexcept or no copy constructor is provided.

The way it should be

C++ has standard std::vector templated container. It mirrors the concepts of the ArrayList (contigious buffer, resizing, index based access). On top of that, it uses everything C++ has to offer. I recommend you to think about this: "When in Rome, do what Romans do".


Pure JavaScript infinite scroll I have a 4 list of posts. I am trying to load more posts (clones of the "original" ones) when I reach the bottom of the container.

class InfiniteScroll {
  constructor() {
    this.observer = null;
    this.isLoading = false;
    this.postsContainer = document.querySelector('#postsContainer');
    this.postsArary = postsContainer.querySelectorAll('.post');
    this.iterationCount = Number(this.postsContainer.dataset.currentPage);
    this.perPage = 5;
    this.maxCount = this.postsContainer?.dataset?.maxCount;
    this.numberOfPages = Math.ceil(this.maxCount / this.perPage);
    this.hasNextPage = this.iterationCount < this.numberOfPages;
  }

  loadMorePosts() {
    if (this.hasNextPage) {
      this.postsArary.forEach(item => {
        let postClone = item.cloneNode(true);
        this.postsContainer.appendChild(postClone);
      });
    } else {
      if (this.observer) this.observer.disconnect();
    }
  }

  getLastPost() {
    const allPosts = postsContainer.querySelectorAll('.post');
    return allPosts[allPosts.length - 1];
  }

  counter() {
    if (this.hasNextPage) {
      this.iterationCount = this.iterationCount + 1;
      this.postsContainer.dataset.currentPage = this.iterationCount;
    }

    this.postsContainer.dataset.hasNextPage = this.hasNextPage;
    this.hasNextPage = this.iterationCount < this.numberOfPages - 1;
  }

  bindLoadMoreObserver() {
    if (this.postsContainer) {
      this.observer = new IntersectionObserver((entries, observer) => {
        entries.forEach(entry => {
          if (entry && entry.isIntersecting) {
            observer.unobserve(entry.target);
            this.loadMorePosts();
            this.counter();
            if (this.hasNextPage) {
              observer.observe(this.getLastPost());
            }
          }
        });
      });

      this.observer.observe(this.getLastPost());
    }
  }

  init() {
    this.bindLoadMoreObserver();
  }
}

const infiniteScroll = new InfiniteScroll();
infiniteScroll.init();
body, body * {
  margin: 0;
  padding: 0;
}

body {
  font-family: Arial, Helvetica, sans-serif;
}

.post {
  margin: 20px;
  padding: 15px;
  border: 1px solid #ccc;
  border-radius: 5px;
}

p {
  line-height: 1.5;
}

Title 1

Lorem ipsum dolor sit amet, consectetur adipisicing elit. Neque dolorum consequatur nostrum sapiente ipsa! Veniam, laudantium accusantium, odio maxime quo adipisci possimus enim quam, voluptate quidem animi perferendis delectus aliquam?

Title 2

Lorem ipsum dolor sit amet, consectetur adipisicing elit. Neque dolorum consequatur nostrum sapiente ipsa! Veniam, laudantium accusantium, odio maxime quo adipisci possimus enim quam, voluptate quidem animi perferendis delectus aliquam?

Title 3

Lorem ipsum dolor sit amet, consectetur adipisicing elit. Neque dolorum consequatur nostrum sapiente ipsa! Veniam, laudantium accusantium, odio maxime quo adipisci possimus enim quam, voluptate quidem animi perferendis delectus aliquam?

Title 4

Lorem ipsum dolor sit amet, consectetur adipisicing elit. Neque dolorum consequatur nostrum sapiente ipsa! Veniam, laudantium accusantium, odio maxime quo adipisci possimus enim quam, voluptate quidem animi perferendis delectus aliquam?

Too complex
Always strive to keep source code complexity as low as possible.
The implementation of InfiniteScroll is way too complex.
It should not be an instantiatable object as it only works if there is one instance, and will only work once.

Page counting need only one variable not 3    this.maxCount, this.numberOfPages and this.hasNextPage can be replace with a single counter. See rewrite pageCount which counts down.

There is no need to store iterationCount or hasNextPage in markup. this.postsContainer.dataset.currentPage = this.iterationCount;

Use Pseudo-class ":last-child" to select last post. querySelector(".post:last-child")

In function getLastPost you reference an undefined variable const allPosts = postsContainer.querySelectorAll(".post");  should be const allPosts = this.postsContainer.querySelectorAll(".post");

The second argument of the IntersectionObserver is a reference to the same object you already have a reference to, there is no need to use the second argument.

When the observer callback is called there will only be one entry that you need to handle. Rather than entries.forEach use entries.find to locate the entry.

Rewrite
The rewrite does not create an object. The function InfiniteScroll holds the observer. When observer.disconnect() is called the IntersectionObserver callback will be de-referenced and the closure containing the InfiniteScroll will be cleaned up automatically.
To prevent InfiniteScroll from being called again it is de-referenced on the first call.
It works the same but with over 65% less code

function InfiniteScroll() {
    InfiniteScroll = undefined;
    const query = (qStr, root = document) => root.querySelector(qStr);
    const lastPost = () => query(".post:last-child", container);

    const perPage = 5;
    const container = query("#postsContainer");
    const posts = container.querySelectorAll(".post");
    var pageCount = container.dataset.maxCount / perPage | 0;

    const observer = new IntersectionObserver(intersect);
    observer.observe(lastPost());

    function intersect(entries) {
        const intersecting = entries.find(entry => entry.isIntersecting);
        if (intersecting) {
            observer.unobserve(intersecting.target);
            posts.forEach(item => container.appendChild(item.cloneNode(true)));
            --pageCount ? observer.observe(lastPost()) : observer.disconnect();
        }
    }
}

InfiniteScroll();
body, body * {
  margin: 0;
  padding: 0;
}

body {
  font-family: Arial, Helvetica, sans-serif;
}

.post {
  margin: 20px;
  padding: 15px;
  border: 1px solid #ccc;
  border-radius: 5px;
}

p {
  line-height: 1.5;
}

Title 1

Lorem ipsum dolor sit amet, consectetur adipisicing elit. Neque dolorum consequatur nostrum sapiente ipsa! Veniam, laudantium accusantium, odio maxime quo adipisci possimus enim quam, voluptate quidem animi perferendis delectus aliquam?

Title 2

Lorem ipsum dolor sit amet, consectetur adipisicing elit. Neque dolorum consequatur nostrum sapiente ipsa! Veniam, laudantium accusantium, odio maxime quo adipisci possimus enim quam, voluptate quidem animi perferendis delectus aliquam?

Title 3

Lorem ipsum dolor sit amet, consectetur adipisicing elit. Neque dolorum consequatur nostrum sapiente ipsa! Veniam, laudantium accusantium, odio maxime quo adipisci possimus enim quam, voluptate quidem animi perferendis delectus aliquam?

Title 4

Lorem ipsum dolor sit amet, consectetur adipisicing elit. Neque dolorum consequatur nostrum sapiente ipsa! Veniam, laudantium accusantium, odio maxime quo adipisci possimus enim quam, voluptate quidem animi perferendis delectus aliquam?


Semi sorted binary search Say you have an array that is "semi-sorted", e.g. something like this:

[7,8,9,10,1,2,3,4,5,6]

Where a part of the end of the sorted array was transferred to the beginning.

Is there a way to still find an element in it in log(n) time?

Here's my solution:

def findVal(arr, val):
    return helper(arr, 0, len(arr) - 1, val)

def helper(arr, start, end, val):
    if (start > end) :
        return False  # empty array

    mid = start + (end - start) // 2

    if (arr[mid] == val):
        return True

    if (mid == 0):
        return False  # 1 element array which is not the value

    rightSorted = arr[mid] <= arr[end]

    if (rightSorted):
        if (val > arr[mid] and val <= arr[end]):
            return helper(arr, mid + 1, end, val)
        else:
            return helper(arr, start, mid - 1, val)

    else:
        if (val < arr[mid] and val >= arr[start]):
            return helper(arr, start, mid - 1, val)
        else:
            return helper(arr, mid + 1, end, val)

I guess you could also do this with a simple while loop instead of recursion.

I spend some more time refining the solutions. Also found a relevant question that asks to find the min/max of the array here.
Problem Context
Searching a value in an array can be up to O(n) in time complexity - you have to iterate through the array and check for each element if it's equal the search value. But - if the array is sorted, this is reduced to O(log(n)) due to a strategy called Binary Search. Binary search allows us at each time cut half of the options to consider. You check the middle element in the array, if the search value is bigger you can discard the lower half, if it's lower you can discard the upper half, and now you do the same for the remaining array - until you either find the value or end up with an empty/1-element array - in that case the element is not in the array.
Here there is a problem that the array is swapped - the question is can we still reduce the time complexity to O(log(n)) ?
The key insight here is that even though the array itself is not perfectly sorted, at least half of it IS - and so we can always tell something about that half, and reduce our scope. This is true for every sub-division we make of the array - at least half of it will be sorted (and in some cases, both halves).
If we are looking for the min/max value, we basically don't care about the part that is sorted, because the pivot value will be in the other half (though the mid value should be included, as it can be the min/max value) - so we can discard the sorted part.
If we are looking for a specific value - we can automatically check if our value is in the range of the sorted half, if it is we discard the non-sorted half, and focus on the sorted. If it's not we focus on the non-sorted - and do the same.
Find Min value
Here is a solution in python - here we check if the right half is sorted. If it is, we reduce our scope to the left half. If not, we reduce our scope to the right half (note that in that case we can safely discard the mid point and move beyond it).
def findMin(arr):
    start = 0
    end = len(arr) - 1
    while(start < end):
        mid = (start + end) // 2
        rightSorted = arr[mid] < arr[end]
        if rightSorted:
            end = mid
        else:
            start = mid + 1
    return arr[start]

The decision to check the right is not arbitrary - it is necessary in case we search for a min value. If we instead search the left, and find out it is sorted - we could not confidently discard that half, as the array might be perfectly sorted - in that case the 0th element is the minimum. So we choose the right in order to be completely sure we still have the min value in the sub array.
Find Max value
In python this is actually very easy - you just need to go one element before the minimum - and even if the min is in 0 position, python interprets -1 as the last element of the array. So you could just use the same code as above, but with return arr[start-1].
Alternatively, we could reverse the min function above:
def findMax(arr):
    start = 0
    end = len(arr) - 1
    while(start < end):
        mid = (start + end) // 2
        leftSorted = arr[mid] > arr[start]
        if leftSorted:
            start = mid
        else:
            end = mid - 1
    return arr[start]

Finding a specific value
If we want to check if a specific value exists in the array - the same key insight hold.
def findVal(arr, val):
    start = 0
    end = len(arr) - 1
    while(start < end):
        mid = (start + end) // 2
        leftSorted = arr[start] <= arr[mid]
        if leftSorted:
            if val >= arr[start] and val <= arr[mid]:
                end = mid
            else:
                start = mid + 1
        else:
            if val >= arr[mid] and val <= arr[end]:
                start = mid
            else:
                end = mid - 1
    return val == arr[start]

A few differences:

we have to check if our value is in the sorted half, and if not discard it.

we have to watch out for the case of 2 elements in an ordered array. The issue here is that if we are not careful, we can end up in an endless loop - because the mid point will be either the 1st or the 2nd, depending if we are using a floor or a ceil operation. Since the default python // operation is a floor, mid will be equal to start, and we have to check the left side in order to either choose the 1st or discard it. [if we use ceil, we should check the right side]


Python code for Mad Libs I am a beginner with only about 2 months under my belt. I'm learning Python 3 using the Automate the Boring Stuff with Python manual. In the chapter on reading and writing files one of the exercises is to make a program that would fill in a mad lib that was read from a file. It works, but it's not very elegant. Can anyone offer me advice on how to better do this task or a better direction to go?

# madLibs.py

import re, os

# define regex
verbRegex = re.compile(r'verb', re.I)
nounRegex = re.compile(r'noun', re.I)
adjectiveRegex = re.compile(r'adjective', re.I)

# change directory
os.chdir('C:\\MyPythonScripts')

# read contents of madLib.txt and store it in variable madLib
madLibFileObject = open('madLib.txt')
madLib = madLibFileObject.read()

y = 0
newMadLib = []

for word in madLib.split():
    newMadLib.append(word)
    if verbRegex.search(word) != None:
        x = input('Enter VERB:  ')
        newMadLib[y] = x
    elif nounRegex.search(word) != None:
        x = input('Enter NOUN:  ')
        newMadLib[y] = x
    elif adjectiveRegex.search(word) != None:
        x = input('Enter ADJECTIVE:  ')
        newMadLib[y] = x
    y += 1

newMadLib = (' ').join(newMadLib)

print(madLib)
print (newMadLib)

It is unnecessary to change the current directory in your python script. Instead you can just open the file with an absolute path - i.e.

madLibFileObject = open("C:\\MyPythonScripts\\madLib.txt")`

To avoid leaking files (madLibFileObject), it it a good idea to use a with statement whenever you open a file, which takes care of automatically closing it for you:

with open("C:\\MyPythonScripts\\madLib.txt") as madLibFileObject:
     madLib = madLibFileObject.read()

Using three regular expressions that each match one word is overkill for this task. It is instead better to use word.lower() == "verb"

for word in madLib.split():
    newMadLib.append(word)
    if word.lower() == "verb":
        x = input('Enter VERB:  ')
        newMadLib[y] = x
    elif word.lower() == "noun":
        x = input('Enter NOUN:  ')
        newMadLib[y] = x
    elif word.lower() == "adjective":
        x = input('Enter ADJECTIVE:  ')
        newMadLib[y] = x

Then, there is clearly quite a bit of code duplication in the above loop, so it should be shortened using a list:

PARTS_OF_SPEECH = ["verb", "adjective", "noun"]
for word in madLib.split():
    if word.lower() in PARTS_OF_SPEECH:
        newMadLib[y] = input("Enter " + word.upper())

Your y counter variable is a reimplementation of the enumerate built-in function, in addition to being poorly named. index would be a better name:

for index, word in enumerate(madLib.split()):

Full code:

PARTS_OF_SPEECH = ["verb", "noun", "adjective"]
with open("C:\\MyPythonScripts\\madLib.txt") as madLibFileObject:
     madLib = madLibFileObject.read()
newMadLib = []
for index, word in enumerate(madLib.split()):
     if word.lower() in PARTS_OF_SPEECH:
        newMadLib[index] = input("Enter " + word.upper())
newMadLib = ' '.join(newMadLib)
print(madLib)
print(newMadLib)


"The Minion Game" challenge This is an implementation of the minion game, which takes in string input and declares a winner between two pre-determined players.

The logic is player 1 gets all sequenced words not starting with a vowel and player 2 gets all sequenced words starting with vowel in the input string.

Objective: performance optimisation. The code passed all the test cases, but I am very unsure where to tune the performance.

For a 1000 letter word, here are the time stats:

real 0m10.142s
user 0m6.386s
sys 0m0.743s

import string
import sys
import itertools

def minion(str):
    person_a_name = 'Stuart'
    person_b_name = 'Kevin'
    letter_list = [a for a in str]
    l = len(letter_list)
    vowel = ['A','E','I','O','U']
    consonants = ['Q','W','R','T','Y','P','S','D','F','G','H','J','K','L','Z','X','C','V','B','N','M']
    all_word = []
    person_a_words = []
    person_b_words = []
    all_word = [letter_list[start:end+1] for start in xrange(l) for end in xrange(start, l)]
    for array in all_word:
        if array[0] in vowel:
            person_b_words.append(array)
    for array in all_word:
        if array[0] in consonants:
            person_a_words.append(array)
    if len(person_a_words) == len(person_b_words):
        print 'Draw'
    if len(person_a_words) > len(person_b_words):
        print person_a_name, len(person_a_words)
    if len(person_b_words) > len(person_a_words):
        print person_b_name, len(person_b_words)

def main():
    str = raw_input()
    minion(str.upper())

if __name__ == '__main__':
    main()

Sample input / output:

banana
Stuart 12

guavaisanotherfruit
Kevin 98

The big performance hit probably comes in this block of code, where you don a bunch of array manipulations and looping:

all_word = [letter_list[start:end+1] for start in xrange(l)
                                     for end in xrange(start, l)]
for array in all_word:
    if array[0] in vowel:
        person_b_words.append(array)
for array in all_word:
    if array[0] in consonants:
        person_a_words.append(array)
if len(person_a_words) == len(person_b_words):
    print 'Draw'
if len(person_a_words) > len(person_b_words):
    print person_a_name, len(person_a_words)
if len(person_b_words) > len(person_a_words):
    print person_b_name, len(person_b_words)

Appending to an array is a (relatively) expensive operation, as is looping over a list. I can see a number of optimisations here. [Edit: I wrote these in the order I thought of them; the big performance gain comes on the last item. I'm leaving the remaining items because they're still instructive, even if not directly applicable here.]

Only loop over all_word once. You can check for starting with a consonant and vowel in the same iteration of the loop:

for array in all_word:
    if array[0] in vowel:
        person_b_words.append(array)
    elif array[0] in consonants:
        person_a_words.append(array)

We've just cut out an iteration over all_word. If all_word is large, that will be a significant saving.
Don't store the words in a list, just the count. All you care about is the relative number of words in each list; the words themselves don't matter. It's much easier to increment an integer than mutate a list, so consider the following:

person_a_words = 0
person_b_words = 0
for array in all_word:
    if array[0] in vowel:
        person_b_words += 1
    elif array[0] in consonants:
        person_a_words += 1

and then you can compare the two integers at the end. That's bound to be a performance saving.
Don't construct all_word as a list; use a generator. If you replace the square brackets with parens:

all_word = (letter_list[start:end+1] for start in xrange(l)
                                     for end in xrange(start, l))

then this becomes a generator comprehension instead of a list comprehension. This means it only creates the elements as they're needed by the for loop; it doesn't create them all in memory before continuing.

Using generators instead of lists is a really good way to reduce occupancy and speed up programs.
Do you even need to use all_word? For each value of start, the first letter of the resulting words will be the same, and this gives you (l - start) different words. You don't actually need to create the words; you just care about their initial letter, and how many distinct words they create.

You could just add the number of distinct words to each person's score directly:

person_a_words = 0
person_b_words = 0
for idx, letter in enumerate(letter_list):
    if letter in vowel:
        person_b_words += len(letter_list) - idx
    else:
        person_a_words += len(letter_list) - idx

That is substantially faster: I just ran this with a 1.5m character string, and it finished in ~1.5s.

Other non-performance related comments:

Don't use str as a variable name; overriding builtins is bad practice.
You've imported the string, sys and itertools modules, but you never use any of them. Why?
PEP 8 requires a space after commas in a list; you should add this in vowel and consonants.
You can get the individual letters of a string by calling list() on it. These calls are equivalent:

letter_list = [a for a in my_string]
letter_list = list(my_string)

although in this case, you don't need to coerce to a list first – you can iterate over the characters of a string directly.
There's no need to assign the length of letter_list to a variable, especially not one with as undescriptive a name as l. It just makes your code harder to read.
The name of your function isn't particularly helpful. Ideally it should give me some idea of what the function does. There should also be a docstring to explain the result.
I would rename the person_a* and person_b* variables to be consonant* and vowel*, respectively – that will make the code easier to read. A and B don't really mean anything (and as evidence, I got them the wrong way round when I first wrote that sentence).
Your function is doing some work (finding out whether there are more vowel sub-words or consonant subwords) and printing to screen (the result). It would be better to separate this into two functions: one that does the work, the other does the result.

That makes it easier to reuse the work of vowels vs. consonants.
To aid readability, I'd keep the same order of variables when you do the comparison at the end. i.e.

if vowel_count > consonant_count:
    print("Vowels victorious!")
elif vowel_count < consonant_count:
    print("Consonants champion!")
else:
    print("Draw!")


program options from command line initialize I'd like to receive a CR for the following command-line program options' initializing using boost::program_options. I'd like to know if there is a better way of doing this using boost::program_options or using another available library/code:

#include <iostream>
#include <boost/program_options.hpp>

using namespace std;
namespace po = boost::program_options;

int main(int ac, char* av[]) {
    /// Command line options initialize
    po::options_description visible_desc("Usage: program [options] [path/]logger_filename");
    po::options_description full_desc;
    po::positional_options_description pd;
    bool verbose, anomaly_detection, analyze_activity;
    string normal_login_word;
    string log_file_path;
    string week_start_day;

    auto generic_group = po::options_description("Generic options");
    generic_group.add_options()
            ("help,h", "produce help message")
            ("verbose", po::value<bool>(&verbose)->default_value(false), "Show detailed times of login.")
            ;

    auto calender_group = po::options_description("Calender options");
    calender_group.add_options()
            ("week-start-day,d", po::value<string>(&week_start_day)->default_value("Monday"), "Week starting day ('--week-start-day help' for a list).")
            ;

    auto log_group = po::options_description("Logger options");
    log_group.add_options()
            ;

    auto hidden_options_group = po::options_description("Logger options");
    hidden_options_group.add_options()
            ("log-path,l", po::value<string>(&log_file_path)->default_value("/home/sherlock/message_from_computer"), "Path to login/logout logger.")
            ;

    auto anomalies_group = po::options_description("Mode options");
    anomalies_group.add_options()
            ("analyze-log", po::value<bool>(&analyze_activity)->default_value(true), "Analyze activity - show activity times and summarise activity.")
            ("anomaly-detection", po::value<bool>(&anomaly_detection)->default_value(false), "Check for anomalies in logger.")
            ("normal-login-word", po::value<string>(&normal_login_word)->default_value("login"), "For anomaly detector- word that should symbol a login line in login/logout logger (after '+' sign).")
             ;

    pd.add("log-path", -1);

    visible_desc.add(generic_group).add(calender_group).add(log_group).add(anomalies_group);
    full_desc.add(generic_group).add(calender_group).add(log_group).add(anomalies_group).add(hidden_options_group);

    /// Command line options applying
    po::variables_map vm;
    po::store(
            po::command_line_parser(ac, av)
            .options(full_desc)
            .positional(pd)
            .run(), vm);
    po::notify(vm);

    /*
     ... Analyze options ...
    */
    return 0;
}

Update: after @pacmaninbw and @ALX23z CRs I created a new updated post: program options from command line initialize [v2 - after CR]

I tried boost::program_options but simply didn't like it.

boost::program_options has the feature that certain options are additive or hidden - which is why their usage requires so much extra work (you have to specify each option you use and state how they are processed). If you are fine without these features, you can work with config files - *.ini format - and simply read and store all option that the file has, no need for the whole mess in the main. It is not hard to implement a config file reader and Windows has built in functions for reading arguments from a .ini file.

Also this way you can supply input parameters via an .ini file instead of command line, which is much more convenient if you have more than 5 parameters and I tend to have dozens in tests.

In my case, I implemented a class ConfigFile that basically wraps a std::map<string,string> whose keys are in format SECTION/NAME; with added functions for getting/setting int/double/string values as well as functionality for reading a whole ini file.

In case you want to read a command line via the ConfigFile class: you can reinterpret cmd arguments and store them inside the ConfigFile class. string-based options simply store as is or optionally add a prefix-section. To support index based options you ought to provide a vector of strings to that interprets them.

I don't know of any open-source library that implements something like this, though I didn't check for any. But it is easy to implement on your own. I might publish my implementation together with a couple of other utility features as an open-source but not now. So...

Example of CMD parsing:

class CConfig
{
public:
            // sKeys is used to store index based inputs of CMD
    void ParseCMD(  int                         argc,
            const   char**                      argv,
                    std::vector<std::string>    sKeys = {});

   std::string  GetString(std::string key,  std::string  defaultValue);
   double       GetDouble(std::string key,  double       defaultValue);
   int          GetInt(   std::string key,  int          defaultValue);

   void         SetString(std::string key,  std::string  value);
   void         SetDouble(std::string key,  double       value);
   void         SetInt(   std::string key,  int          value);

private:
    std::mutex                          m_mutex;
    std::map<std::string, std::string>  m_map;
};

void CConfig::ParseCMD( int                         argc,
                const   char**                      argv,
                        std::vector<std::string>    sKeys)
    {
        std::lock_guard<std::mutex> g(m_mutex);
        size_t index = 0;
        for (int i = 1; i < argc; i++)
        {
            if (argv[i][0] != '-')
            {
                if (sKeys.size() <= index)
                {
                    std::cout "\n[warning]: unassigned input parameters; total number of index-based input parameters is: " << sKeys.size();
                    continue;
                }

                m_map[sKeys[index]] = argv[i][0];
                continue;
            }

            if (i + 1 < argc && argv[i + 1][0] != '-')
            {
                m_map[argv[i]+1] = argv[i + 1];
                i++;
            }
            else
            {
                // simply defines this key with empty value
                m_map[argv[i]+1] = {};
            }
        }

        if (sKeys.size() > index)
        {
            // or add option for making it into an error and not just a warning print
            std::cout << "\n[warning]: not all index-based values were assigned");
        }
    }

// in main simply write:
   int main(int argc, const char* argv[])
   {
         CConfig config;
         config.ParseCMD(argc, argv, {/*list of strings for identification*/});
         ...
   }


Is my Java SQL connection secure from hackers? I would like to know if my java db class is enough protected against hackers.  (I'm currently developing an Android application).  I protect it with a infos.properties file which contains every information that I need.

final class DB {
static final String DRIVER;
static final String URL;
static final String USER;
static final String PASSWORD;

static {
Properties prop = new Properties();
try {
    prop.load(new FileInputStream("infos.properties"));
} catch (IOException e) {
    e.printStackTrace();
}
DRIVER = prop.getProperty("DRIVER");
URL = prop.getProperty("URL");
USER = prop.getProperty("user");
PASSWORD = prop.getProperty("password");
}

public static ResultSet doQuery(String query)
{
    try {
        Class.forName(DRIVER);
        Connection conn = DriverManager.getConnection(URL, USER, PASSWORD);
        Statement stmt = conn.createStatement();
        ResultSet rs = stmt.executeQuery(query);
        if (rs != null) rs.close();
        if (stmt != null) stmt.close();
        if (conn != null) conn.close();
        return (rs);
    } catch (ClassNotFoundException e) {
        e.printStackTrace();
    } catch (SQLException e) {
        e.printStackTrace();
    }
    return null;
}
}

If it is not enough protected, what should I do then ?

Do not store the connection information in a file unencrypted
Assuming that you are connecting to a database somewhere on the internet... If you plan on distributing this application, do not store the connection information unencrypted in a file on the user's device. In fact, preferably don't store the database connection information at all.
Do not let your Android application connect directly to your remote database!
It is not recommended to let your Android application, or any other distributed application, connect directly to your database. This is because it is very easy to decompile an application, which would give information about the connection details, your database structure, and a whole lot of other things.
Instead, perform connections to a server-side script or application that is responsible for handling the connection between your Android application and your database. Only let this script/application that only you control deal with the database connection.
Unless you deal with this, any number of prepared statements your application itself uses doesn't matter, as a user could just find out the connection details from your application and then create their own connection to the database server.


Are there any open vulnerabilities in this mailer script? I made a PHP mailer script does the basic validation of fields, return errors, else submit if all is good. But it also has a honeypot field that is not required to be filled in (I'm assuming by hiding it using CSS a spambot will fill in the field anyway).  If the field is not empty, it opens a text file and writes/appends the attempt on it, and it also sends an email alert of the attempt.

<?php
//print_r($_POST);
$error['name'] ="";
$error['company']="";
$error['email'] ="";
$error['subject'] ="";
$error['message'] ="";
$error['website'] ="";
$success = "";
$thistime = time();
$current_date = date('m/d/Y/T ==> H:i:s');

if(isset($_POST['_save'])) {
    $name = stripslashes($_POST['name']);
    $email = stripslashes($_POST['email']);
    $company = stripslashes($_POST['company']);
    $message = stripslashes($_POST['message']);
    $subject = stripslashes($_POST['subject']);
    $website = stripslashes($_POST['website']);

    if (empty($name) || empty($email) || empty($subject) || empty($message) ||
            !empty($website)) {
    if (empty($name))
       $error['name'] = "Please enter your Full Name";
    if (empty($email))
       $error['email'] = "Please enter a valid Email Address";
    if (empty($company))
       $error['company'] = "Please enter Your Company Name";
    if (empty($subject))
       $error['subject'] = "Please Write a Subject";
    if (empty($message))
       $error['message'] = "Please write a message, inquiries or
               other concerns above";
    if (!empty($website)) // this is the honeypot
       $error['subject'] = "Opps looks like you're a spambot. You
              just filled in a not required field.;
      $myFile = "botlog.txt";
      $fh = fopen($myFile, 'a') or die("can't open file");
      $stringData = "bot trapped" . " " . "-"  . " " . $website . " " . "-
                    " . " " . $current_date . "\r\n";
      fwrite($fh, $stringData);
      fclose($fh);
      $donot="donotreply@example.com";
      $headers="From: {$email}\r\nReply-To: {$donot}"; //create headers
      mail('opps@example.com',$headers,$stringData);
    }
    else { //if not empty
    stripslashes($headers);
    $headers="From: {$email}\r\nReply-To: {$email}"; //create headers
    $content="Name: ".$name."\r\n\r\nCompany: "
               .$company."\r\n\r\nSubject: ".$subject."\r\n\r\nMessage: ".$message;
    mail('opps@example.com',$subject,$content,$headers); //mails it
    $success = "Thank you! You're email has been sent.";
    #done;
    }
}
?>

Am I doing it right? Does this open any vulnerabilities? I'm open to any suggestions and improvements.

Your mailer is vulnerable to a header-splitting attack!  (Also known as a CRLF-injection attack.)

Basically, if one of the fields contains a CRLF, the attacker can insert any arbitrary header into the generated message.  For example, if the email parameter is

victim1@example.com%0d%0aCc:%20victim2@example.net

… then you will compose a message with headers

From: victim1@example.com
Cc: victim2@example.net
Reply-To: victim1@example.com
Cc: victim2@example.net

PHP's mail() function will "helpfully" add victim2@example.net as a spam recipient.  The spam will look like it came from victim1@example.com.

The simplest mitigation, I believe, is to pass all of the header fields through
mb_encode_mimeheader() first.

  General warning regarding string concatenation and interpolation

  Any time you compose a string that will be interpreted by another computer system, be it an e-mail message, HTML page, or SQL query, assume that you are vulnerable to some kind of injection attack.  Every single one of these "human-friendly" languages (as opposed to binary formats such as JPEG images) will have delimiters of special significance.  Header-splitting, HTML/JavaScript injection, and SQL injection attacks all have the same root cause: careless string concatenation or interpolation.

  Therefore, before concatenating or interpolating strings, stop and think: "What is the appropriate escaping mechanism that I should be using?"  Better yet, ask: "Is there a higher-level library that will let me accomplish the task without low-level string manipulation?"

  RFC time!

  I'm diving into a deeper technical discussion about why mb_encode_mimeheader() is my choice for escaping.  If you get confused by this technical discussion, you can safely ignore this explanation and just filter all the input through mb_encode_mimeheader().

  There are actually two relevant standards for e-mail headers.  Mail Transfer Agents (MTAs) care about RFC 2822.  Mail User Agents (MUAs) care about RFC 2047.

  MTAs are completely oblivious to MIME encoding.  As long as they see the headers they need (e.g. "From", "To", "Return-Path"), and the headers contain a safe subset of ASCII, they are happy.

  MUAs, on the other hand, are more sophisticated, in that they have to handle non-ASCII text for the benefit of non-English users.  For example, a "Subject" header could contain arbitrary text, such as "¡Hóla!".  Such a Subject line should be encoded using

mb_encode_mimeheader("¡Hóla!", "UTF-8", "Q");

  as

=?UTF-8?Q?=C2=A1H=C3=B3la!?=

  That is, by the way, the way to ensure that the $subject parameter is RFC 2047-compliant, as specified in the mail() documentation.

  What about headers that are relevant to both MTAs and MUAs, such as the "From" header?  One might want to generate a header that looks to the user like

From: "François Müller" <francois.mueller@example.com>

  Over the wire, such a header must be encoded, since it contains non-ASCII characters.  One acceptable encoding would be

From: "=?UTF-8?Q?Fran=C3=A7ois=20M=C3=BCller?=" <francois.mueller@example.com>

  Such an encoding satisfies both RFC 2822 (since the MTA sees an opaque ASCII string between the double-quotes) and RFC 2047 (since the MUA should know how to decode the MIME-encoded string for display).  To achieve such a result, you would need PHP code such as

$from_header = sprintf('From: "%s" <%s>',
                       mb_encode_mimeheader("François Müller", "UTF-8", "Q"),
                       "francois.mueller@example.com");

  If you write your code as

$email = '"François Müller" <francois.mueller@example.com>';
$from_header = mb_encode_mimeheader("From: $email", "UTF-8", "Q");

  … you'll get

From: =?UTF-8?Q?=22Fran=C3=A7ois=20M=C3=BCller=22=20=3Cfrancois=2Emueller?=
 =?UTF-8?Q?=40example=2Ecom=3E?=

  … which is wrong, since the MTA sees one opaque ASCII string that does not look like a valid e-mail address.  However, the main point is that such code, though simplistic, is not vulnerable to header injection.  As long as the $email variable contains simple ASCII text, it will work.  For example,

$email = '<francois.mueller@example.com>';
$from_header = mb_encode_mimeheader("From: $email", "UTF-8", "Q");

  … passes the text through unchanged, producing

From: <francois.mueller@example.com>

  PHP-bashing time!

  As is often the case with PHP, the mail() function is poorly designed, such that it is easy to fall into this trap.  Expecting programmers to know the RFCs and take the time to encode all of the headers properly is pure madness!  If mail() were designed such that it accepted an associative array of (header name → header value) pairs, then it could automatically apply mitigation against header-splitting attacks.


Reinventing the basic calculator functions I decided to reinvent the basic calculator functions using only:

the set of positive integers
greater than, less than, and equality properties
increment and decrement

(in other words, the counting on fingers method)

def is_positive_int(x):
    if type(x) == int and x >= 0:
        return True
    else:
        return False

def add(x,y):
    if is_positive_int(x) and is_positive_int(y):
        for i in range(y):
            x += 1
        return x
    else:
        return False

def multiply(x,y):
    if is_positive_int(y) and is_positive_int(y):
        m = 0
        for i in range(y):
            m = add(m,x)
        return m
    else:
        return False

def power(x,y):
    """ x to the y power """
    if is_positive_int(x) and is_positive_int(y):
        p = 1
        for i in range(y):
            p = multiply(p,x)
        return p
    else:
        return False

def subtract(x,y):
    """ x - y """
    if is_positive_int(x) and is_positive_int(y) and x > y:
        for i in range(y):
            x -= 1
        return x
    else:
        return False

def divide(x,y):
    """ x / y """
    if is_positive_int(x) and is_positive_int(y):
        floor = 0
        remainder = 0
        for i in range(x):
            remainder += 1
            if remainder == y:
                floor += 1
                remainder = 0
        return {'floor':floor, 'modulus':remainder}
    else:
        return False

def floor(x,y):
    """ a // b"""
    return divide(x,y)['floor']

def modulus(x,y):
    """ x % y"""
    return divide(x,y)['modulus']

Let's say you have this situation:

if ...:
    ...
    return ...
else:
    return ...

What if the if condition is True?  Some statements will be executed, and then the function will return.  In other words, if the condition is True, execution never leaves the if block.  Therefore, you really don't need the else.  Just put the return directly under the if:

if ...:
    ...
    return ...
return ...

It is generally a bad idea to use the pattern type(...) == ....  Usually, we use isinstance(..., ...).  That way, subclasses are allowed.  Of course, you might keep it if you don't want subclasses to be allowed, but I don't see a reason to restrict the user.  I'm assuming you want only integers because only integers can be passed to range, but subclasses of integers can indeed also be passed.  For example, booleans are integers.  range(False, True) is perfectly valid.  Also, True + False + False is valid, so it won't mess up your arithmetic.  That makes your is_positive_int function look like this:

def is_positive_int(x):
    if isinstance(x, int) and x >= 0:
        return True
    return False

But what do you see?  If a condition is True, we return True.  If it is False, we return False.  In other words, we are really just returning the condition, so why not do it like this:

def is_positive_int(x):
    return isinstance(x, int) and x >= 0

I see a very similar pattern in all of your functions:

if is_positive_int(x) and is_positive_int(y):
    ...
else:
    return False

First of all, we can remember what I mentioned earlier about not leaving the if block unless the condition is False.  Therefore, I would actually switch the if and else:

if not is_positive_int(x) or not is_positive_int(y):
    return False
...

We remove a level of nesting that way.  The problem is that we use the same code in a bunch of different functions, so we can make a decorator.  A decorator is a function which, when given another function (a) to wrap, returns a wrapper function (b) for a.  b overrides a and does its special thing, but it usually calls a later.  This is my idea:

def guarantee_positive(function):
    # Each function takes two arguments
    def wrapper(x, y):
        if is_positive_int(x) and is_positive_int(y):
            # Deal with it normally
            return function(x, y)
        return False
    # Now wrapper(x, y) will be called instead of function(x, y)
    return wrapper

To use it, add @guarantee_positive before each function definition.  For example:

@guarantee_positive
def divide(x, y):
    floor = 0
    remainder = 0
    for i in range(x):
        remainder += 1
        if remainder == y:
            floor += 1
            remainder = 0
    return {'floor': floor, 'modulus': remainder}

That makes for a much cleaner function.  Of course, subtract has an extra condition, but I'll get to that later.

With all that about function decorators, why do you need them both to be positive?  I can see why you might want y to be positive: because range doesn't work the same with negative numbers, but you aren't using i most of the time, so just use the absolute value of y.  I'll give an example:

def multiply(x, y):
    m = 0
    function = add if y > 0 else subtract
    for _ in range(abs(y)):
        m = function(m, x)
    return m

It doesn't require a decorator, and it is capable of more.  You can now use it with any combination of negative and positive:

-3,  3 -> -9
-3, -3 ->  9
 3, -3 -> -9
 3,  3 ->  9

Your add function would be like this:

def add(x, y):
    # Add 1 if y is positive; subtract 1 if it is negative
    y_add = (1 if y >= 0 else -1)
    for _ in range(abs(y)):
        x += y_add
    return x

Again, it's shorter than the original, and it does more.

There are some functions where it isn't quite that easy.  For example, power() would be difficult to implement a negative y (x doesn't need to be positive even with the current implementation), but most of the others don't need the decorator.  I won't tell you all of them, but I have given examples of the more common hangups.

Back to the decorator, I think it is better to raise an exception when something is wrong than to return False.  After all, they are called exceptions for a reason.  Normally, the function does one thing, but there are exceptions (or there should be).

I see a lot of for i in range(y) when i isn't used.  The common way of signifying that i is just a place holder is using _ as the variable name.

It looks like you cheated with divide() :)  You don't actually have division, you have divmod.  I think a name similar to that would be more appropriate.  another thing is that you return a dictionary.  I prefer something closer to how the built-in function does it:

return floor, remainder

That way, you can do:

floor, remainder = divide(...)

Instead of the equivalent:

result = divide(...)
floor, remainder = result['float'], result['modulus']

Either one should have a doc string that explains what the return value is.  If I called divide(...) when it had a doc string of just x / y, I would assume that I would get x / y, not a dictionary with the floored result and the remainder.  Your floor and modulus functions would then be:

def floor(x, y):
    """a // b"""
    return divide(x, y)[0]

def modulus(x, y):
    """x % y"""
    return divide(x, y)[1]

Except that divide will hopefully have a more descriptive name.

I did notice that floor is inside of divide(), but I am assuming that is just a typo.

You seem to be following PEP 8 fairly well, but I noticed that you don't have any whitespace between the key-value pairs in your dictionary.  PEP 8 recommends that you put a space between the colon and the value.  I don't see it in the PEP, but I think you should put a space between a comma and the following object.  For example:

def divide(x, y):

not

def divide(x,y):

Without the space, it feels smooshed, like you're golfing (code golfing, not with a golf club).


Conditional validation for credit card payment information on form submission This is the piece of code I'd like to refactor. Basically, when the credit card option is selected, then do I want the credit card number, zip code, and Cvv inputs to be validated.

validateForm=(e)=> {
  Array.from(inputElements).forEach(element=> {
    let value=element.value;
    let input=element;
    if (input===name) {
      errorMessage(isValidName, input, value, e)
    } else if (element===email) {
      errorMessage(isValidEmail, input, value, e);
    } else if (element===creditNumber) {
      if (creditOptionChecked()) {
        errorMessage(isValidCreditCard, input, value, e);
      }
    } else if (element===zip) {
      if (creditOptionChecked()) {
        errorMessage(isValidZipCode, input, value, e);
      }
    } else if (element===cvv) {
      if (creditOptionChecked()) {
        errorMessage(isValidCvv, input, value, e);
      }
    } else if (element==fieldSetActivity) {
      validateCheckboxes(e)
    }
  });
}

form.addEventListener('submit', (e)=> {
  validateForm(e)
  console.log("hello")
})

I've been at this for a couple hours, but I'm still a bit new to be able to implement something that is modular. Here is a link to my JS fiddle, so you can see the rest of the javascript code: https://jsfiddle.net/apasric4/4xfL9utv/2/

Incomplete code

Your question's code is missing so much information there is not much to review.

Because of this I can only review the code in regard to exactly the logic your function performs.

Style points

Don't add code that is not needed. Eg

The variable input is a copy of element having two names for the same reference will lead to confusion, mistakes and bugs.
You add a function to form submit, and all it does is call validateForm. Set the listener to validateForm eg form.addEventListener('submit', validateForm) and avoid the middle man.

Good code does not repeat AKA is DRY (Don't Repeat Yourself).

Examples of repeated code;

You call errorMessage, with the same 3 last arguments five times. Create a function and just pass the first argument, let the function call errorMessage with the rest of the arguments.
You repeat the same statement if (creditOptionChecked()) { three times. Again hand the repetitive source code to a function..

Variables that do not change and/or should not change should be declared as constants. eg const value = element.value; rather than let value = element.value;
Array.from requires an array like argument. Array like arguments are all iterable thus you do not need to create an array before you iterate. The line Array.from(inputElements).forEach(element=> { can be written as inputElements.forEach(element => { or for (const element of inputElements) {

Array iterators (example Array.forEach) are not as efficient as for or while loops. Using for or while loops when you can also gives you a better sense of the code complexity, while using a little less memory and power.

Example

Rewriting your function to be compatible with the original under all possible unknown states using the above points.

form.addEventListener("submit", e => {
    var input;
    const error = valid => errorMessage(valid, input, input.value, e);
    const optError = valid => creditOptionChecked() && error(valid);
    for (input of inputElements) {
        if (input === name) { error(isValidName) }
        else if (input === email) { error(isValidEmail) }
        else if (input === creditNumber) { optError(isValidCreditCard) }
        else if (input === zip) { optError(isValidZipCode) }
        else if (input === cvv) { optError(isValidCvv) }
        else if (input == fieldSetActivity) { validateCheckboxes(e) }
    }
});

There are some improvements that can be made depending on the unknowns, maybe needing no if statements at all, but I likely waste both of our times if I speculate.


Searching two different elements in a same list I have a list of conversion histories, where information about user channels is stored. For example user may have app for iOS and Android installed. Each time user logs in, I store a conversion history, so that there might be multiple rows with shared data (e.g. client is always iOS).

I need to get the latest usage of iOS and Android in this case, as you can see code is pretty much in 'serial' way, so I wonder how this part can be optimized. Any help is appreciated.

private List<UserChannel.AppUser> findLatestAppClients(
            List<Conversion> conversions
    ) {
        Optional<Conversion> iosClient =
                conversions.stream().filter(c -> IOS.clientName().equals(c.getClient())).findFirst();

        Optional<Conversion> androidClient =
                conversions.stream().filter(c -> ANDROID.clientName().equals(c.getClient())).findFirst();

        List<UserChannel.AppUser> appUsers = new ArrayList<>();

        if (iosClient.isPresent()) {
            appUsers.add(new UserChannel.AppUser(iosClient.get().getClient(), iosClient.get().getClientId()));
        }

        if (androidClient.isPresent()) {
            appUsers.add(new UserChannel.AppUser(androidClient.get().getClient(), androidClient.get().getClientId()));
        }

        return appUsers;
    }

There's no problem with the performance. Java 8 filtering actually doesn't scan the whole stream, so the processing will be done only until the first element meeting the predicate is hit. So you're fine this way. However, I would do a bit of refractoring here:

private List<UserChannel.AppUser> findLatestAppClients(
            List<Conversion> conversions) {
    Optional<Conversion> iosClient = findFirstWithName(IOS.clientName());

    Optional<Conversion> androidClient = findFirstWithName(ANDROID.clientName());

    List<UserChannel.AppUser> appUsers = new ArrayList<>();

    addIfPresent(appUsers, iosClient);

    addIfPresent(appUsers, androidClient);

    return appUsers;
}

private Optional<Conversion> findFirstWithName(String name){
    return conversions.stream().filter(c -> name.equals(c.getClient())).findFirst();
}

private void addIfPresent(List<UserChannel.AppUser> appUsers, Optional<Conversion> conversion){
    if (conversion.isPresent()) {
        appUsers.add(new UserChannel.AppUser(conversion.get().getClient(),
            conversion.get().getClientId()));
    }
}


High performance exponential of a skew Hermitian matrix in Fortran 95 I'm new to Fortran, and this is pretty much my first escapade. Below is a function that I wrote which relies on calls to LAPACK. The function is sat in a module with some other functions and works perfectly, but seeing as this is really the workhorse of the program I'm building I want to squeeze it hard for performance. How does it look? Am I doing anything stupid, or would another call to BLAS or LAPACK somewhere improve the performance?

The function takes a Hermitian matrix H, and returns the matrix exponential of the skew-Hermitian matrix -iHt where i is the imaginary number and t is a real number. (This is the solution to the Schrodinger equation for a time independent Hamiltonian.) s, the length of one side of the Hamiltonian, is included for automatic array initialisation rather than using allocations. Typically it'll be less than 10.

function time_indep_schrodinger(s,H,t)
    ! s : The length of one side of H.
    ! H : The Hamiltonian.
    ! t : The time t.
    !
    ! The LAPACK subroutine zheev requires a tridiagonal
    ! subcopy of H. This array is then transformed into
    ! a matrix of eigenvectors. This also yields the
    ! eigenvalues as a 1D array, which are then
    ! exponentiated before using the matrices of
    ! eigenvectors to produce the evolution operator.
    !
    ! Finds the eigenvalues of Ht, then uses U exp(-i*eigs) U^H.

    integer,    intent(in) :: s
    complex*16, intent(in) :: H(s,s)
    real*8,     intent(in) :: t
    complex*16             :: B(s,s), eigv(s,s), time_indep_schrodinger(s,s), work(2*s-1)
    real*8                 :: rwork(3*s-2), eigs(s)
    integer                :: info, n, m

    ! Hermitian matrix to diagonalise.
    forall (n=1:s, m=1:s, m>=n) eigv(n,m) = H(n,m)*t

    ! Get eigenvectors and eigenvalues.
    call zheev('V','U',s,eigv,s,eigs,work,2*s-1,rwork,info)

    ! Scale columns of copied matrix be exponentiated eigenvalues (with -i).
    do n=1,s
      B(:,n) = eigv(:,n)*exp((0,-1)*eigs(n))
    end do

    ! Finally multiply scaled eigenvectors with conjugate of eigv.
    time_indep_schrodinger = matmul(B,conjg(transpose(eigv)))

end function

Your code looks good. When s is small, there should be no need to use BLAS instead of matmul(). I like you exploit the shorthand array notation and the forall construct.

Debatable is your use of explicit shaped arrays. They could be very slightly faster, because they are contiguous, but in many cases the compiler will have to copy the arrays when calling your routines and the overall code will be slower. The preferred way in modern code are assumed shape arrays, e.g.

 complex(complex_kind), intent(in) :: H(:,:)
 complex(complex_kind)             :: B(1:ubound(H,1),1:ubound(H,1))

but there may be reasons to use your way and it may be shorter.

Generally avoid using star notation real*4 *8 *16 for sizes of your variables. It is non-standard and obsolete. Use real(some_kind_constant).

You can use selected_real_kind() and selected_int_kind() (now preferred), to get the constants.

Or if you don't mind Fortran 2008 features and need to know the size in bits you can use kind constants from the iso_fortran_env module, like integer(int32), real(real64) and so on.

Another possibility from Fortran 2003 is to use kind constants from the iso_c_binding module, like integer(c_int) if you need interoperability with C code.


Restructuring a csv file with d3js In my code below I have restructured the following csv file from something like this:

date,type
2017-01,E
2017-01,E
2017-01,E
2017-01,U

To this:

{date: "2017-01", E: 3, U: 1}

Using d3.nest() with a rollup method.

The only thing is that I'm mapping out the type values somewhat manually like this:

var lineData = nestData.map(function(d) {
  return {date: d.key, E: d.values[0].value, U: d.values[1].value};
});

always expecting the types to be in order. I do this by simply sorting the data by type and then re-sort by date again.

What I basically want to know is if there's a better way of achieving this or if there's a step or two that I can omit from the code making it more efficient.

Here's all the code:

var csvData =
`date,type
2017-01,E
2017-01,E
2017-01,E
2017-01,U
2017-01,U
2017-02,E
2017-02,E
2017-02,U
2017-02,U
2017-02,U
2017-03,U
2017-03,U
2017-03,E
2017-03,E
2017-03,E
2017-03,E`;

var durations = 0;

var parseTime = d3.timeParse("%Y-%m");

var margin = {top: 25, right: 25, bottom: 25, left: 25},
    width = 420 - margin.left - margin.right,
    height = 185 - margin.top - margin.bottom;

var g = d3.select("#chart").append("svg")
  .attr("width", width + margin.left + margin.right)
  .attr("height", height + margin.top + margin.bottom)
.append("g")
  .attr("transform",
    "translate(" + margin.left + "," + margin.top + ")");

var x = d3.scaleTime().range([0, width]),
    y = d3.scaleLinear().range([height, 0]),
    z = d3.scaleOrdinal(d3.schemeCategory10);

var xAxis = d3.axisBottom(x)
    .tickFormat(d3.timeFormat("%B"))
    .ticks(3);

var yAxis = d3.axisLeft(y).ticks(4);

var line = d3.line()
    .curve(d3.curveCardinal)
    .x(function(d) { return x(d.date); })
    .y(function(d) { return y(d.lines); });

g.append("g")
    .attr("class", "axis axis--x")
    .attr("transform", "translate(0," + height + ")");

g.append("g")
    .attr("class", "axis axis--y");

var data = d3.csvParse(csvData, function(d) {
  d.date = d.date;
  d.type = d.type
  return d;
}).sort((a, b) => a.type - b.type)

  var nestData = d3.nest()
    .key(d => d.date)
    .key(d => d.type)
    .rollup(leaves => leaves.length)
    .entries(data);

  console.log("Nested: ", nestData)

  var lineData = nestData.map(function(d) {
    return {date: d.key, E: d.values[0].value, U: d.values[1].value};
  });

  lineData.forEach(function(d) {
    d.date = parseTime(d.date);
  })

  lineData.sort((a, b) => a.date - b.date)

  console.log("Mapped: ", lineData)

  parsed(lineData);

  function parsed(dataTest) {

    var keys = ["E","U"]

    var newData = keys.map(function(id) {
      return {
        id: id,
        values: dataTest.map(function(d) {
          return {date: d.date, lines: d[id]};
        })
      };
    });

    console.log("Mapped w/key: ", newData)

    z.domain(newData.map(function(c) { return c.id; }));

    var max = d3.max(dataTest, d => d3.max([d.E, d.U]));

    y.domain([0, max]).nice();

    x.domain(d3.extent(dataTest, d => d.date));

    g.selectAll(".axis.axis--y").transition()
      .duration(durations)
      .call(yAxis);

    g.selectAll(".axis.axis--x").transition()
        .duration(durations)
        .call(xAxis);

    var lineValues = g.selectAll(".lineValues")
      .data(newData);

    lineValues = lineValues
      .enter()
    .append("path")
      .attr("class", "line lineValues")
      .style("stroke", function(d) { return z(d.id); })
      .merge(lineValues);

    lineValues.transition()
      .duration(durations)
      .attr("d", function(d) { return line(d.values) })

  }
.line {
  fill: none;
  stroke: steelblue;
  stroke-width: 1.5px;
}

<meta charset ="utf-8">

First of all: drop the row function, which is the anonymous function as the second argument of csvParse...

var data = d3.csvParse(csvData, function(d) {
    d.date = d.date;
    d.type = d.type
    return d;
})

... because you are not changing anything!

So, it can be just:

var data = d3.csvParse(csvData);

Back to your question:

Indeed, you don't need to reference the array by the indices, which forces you to sort the data array and then to sort the resulting array... that's not only confusing and error-prone, but also very computer-intensive for the browser: sorting big arrays of objects can take quite some time. It's a good idea avoiding sorting, specially because, as we'll see, you don't need it.

There is a way to omit that step, making it automatic. Better than that, you can make the whole thing automatic!

In my proposed solution, you don't even need to set the keys, as you do right now...

var keys = ["E","U"];

You can easily get all the unique type values:

var keys = [...new Set(data.map(function(d) {
    return d.type
}))];

And then, creating your lineData:

var lineData = nestData.map(function(d) {
    var obj = {};
    obj.date = d.key;
    keys.forEach(function(e) {
        obj[e] = d.values.find(function(f) {
            return f.key === e
        }).value
    });
    return obj;
});

The above code will work for any number of different types you have, and since you already have the dates in the chronological order, you don't need any sort.

This, in short, is what the function does:

For each map interaction we create a new object:

var obj = {};

We set the key/value pair for the dates, since there is always a date:

obj.date = d.key;

Then, for each value in the keys array, we find the respective value in the values array:

keys.forEach(function(e) {
    obj[e] = d.values.find(function(f) {
        return f.key === e
    }).value
});

Finally, we return the object:

return obj;

Here is a demo, using only the relevant parts in your code:

var csvData = `date,type
2017-01,E
2017-01,E
2017-01,E
2017-01,U
2017-01,U
2017-02,E
2017-02,E
2017-02,U
2017-02,U
2017-02,U
2017-03,U
2017-03,U
2017-03,E
2017-03,E
2017-03,E
2017-03,E`;

var parseTime = d3.timeParse("%Y-%m");

var data = d3.csvParse(csvData);

var keys = [...new Set(data.map(function(d) {
  return d.type
}))];

var nestData = d3.nest()
  .key(d => d.date)
  .key(d => d.type)
  .rollup(leaves => leaves.length)
  .entries(data);

var lineData = nestData.map(function(d) {
  var obj = {};
  obj.date = d.key;
  keys.forEach(function(e) {
    obj[e] = d.values.find(function(f) {
      return f.key === e
    }).value
  });
  return obj;
});

lineData.forEach(function(d) {
  d.date = parseTime(d.date);
})

console.log("Nested: ", nestData)

console.log("Mapped: ", lineData)
<script src="https://d3js.org/d3.v4.min.js"></script>
<svg></svg>

Now, using this code, you can automatically create the adequate lineData array regardless the number and the order of the type properties in your data array.


Better Filtering on Java Streams I need to run filtering on nested Lists and return a List of items from back

I will put a sample code here with my model

import lombok.Data;

import java.util.List;
import java.util.stream.Collectors;

class Scratch {
    public static void main(String[] args) {

        ParentModel parent = new ParentModel();
        List<Information> informationList = parent.getChildDatas().stream()
                //Filtering null && empty grandchild
                .filter(childData -> childData.getGrandChildDatas() !=null && !childData.getGrandChildDatas().isEmpty())
                .flatMap(childData -> childData.getGrandChildDatas().stream() )
                //Filtering null && empty information
                .filter(grandChild -> grandChild.getInformations() !=null && !grandChild.getInformations().isEmpty())
                .flatMap(grandChild-> grandChild.getInformations().stream())
                //filtering key
                .filter(information -> information.getKey().equals("SOMETHING"))
                .collect(Collectors.toList());

    }

    @Data
    static class ParentModel {
        private List<ChildModel> childDatas;
    }

    @Data
    static class ChildModel {
        private List<GrandChildModel> grandChildDatas;
    }

    @Data
    static class GrandChildModel {
        private List<Information> informations;
    }

    @Data
    static class Information{
        private String info1;
        private String info2;
        private String key;
    }
}

So here you can see, I'm filtering and mapping it back to child streams two times.

Is there alternate way to achieve what I'm trying to do here ??

First, you should avoid using null to represent an empty list.  Use an actual empty list, or even Collections.emptyList(), which is a immutable singleton.  This will avoid the need for null checks.

Second, you are filtering by getting a sub attribute, checking conditions on that object, then requesting the same sub attribute in the next step.  Instead, you should map to the sub attribute, then filter & process.

    ...
    .map(ChildModel::getChildrenDatas)
    .filter(data -> data != null && !data.isEmpty())
    .flatMap(List::stream)
    ...

Third, the .isEmpty() check is unnecessary, as .flatMap works fine with an empty stream.

   ...
    .map(ChildModel::getChildrenDatas)
    .filter(Objects::nonNull)
    .flatMap(List::stream)
    ...

Again, if you don’t use null to represent empty lists, the filter line can be removed entirely.


Creating and sending a Fixed-Width file This function will run every x seconds.

PAN: The name we are calling our fixed width file we are transmitting.

Private Sub Start()

    ' Get the data from the API so we can get ready to make the PAN File
    Dim Baker As New PAN.Maker
    Baker.SetIngredients(GetAPIData)

    ' Make the PAN file
    Baker.Make()

    ' Retrieve the PAN file from the baker
    Dim PAN As PANv1 = Baker.GetPAN()

    ' Transmit the PAN file, via FTP
    Dim PANSender As New PAN.Transmitter(GetFTPDetails)
    PANSender.Send(PAN)

End Sub

I create the Objects Maker and Transmitter in a PAN namespace to group the functionality. Is this wise?
I decide to use a method to set the data it needs before it can create the PAN file, rather than using a constructor. What is the preferred method in this case?
Am I using too many Objects to handle these tasks, when a more procedural structure would do?
Is the naming of the Baker and Transmitter confusing because they differ from the Objects name?
Are these straightforward and easily understandable OOP practices?

This function will run every x seconds.

That's great. But without more context, there is no way we can identify any potential issues with the code you've provided.

Private Sub Start()

Depending on what the containing class is called, that method's name isn't very clear. And why is it Private? Who's calling it? A method like Start() sounds like something that belongs on the public interface; I'm confused.

If I were maintaining this code, I would remove every single comment you have there. Why? Because every single one of these comments is restating what the code does, and good comments should say why the code does what it's doing.

  I create the Objects Maker and Transmitter in a PAN namespace to group the functionality. Is this wise?

It looks like you have very specialized objects, and that is very good thing. Whether or not they deserve their own namespace isn't something that can be commented on, because we have no idea what other things you have here: for all I know every type involved in what you've shown could be in the same namespace, it depends on the scope of your application: if it's an ERP system with 4,327 other functionalities, then yes, definitely put that in its own namespace. If it's a specialized application that only deals with "PAN" files, I don't know.

  I decide to use a method to set the data it needs before it can create the PAN file, rather than using a constructor. What is the preferred method in this case?

I'm guessing you're referring to this line:

Baker.SetIngredients(GetAPIData)

What's GetAPIData? It looks like it could be a member call (a property getter?), or a field - in either case, the name is a bad one: GetAPIData would be a name for a method. Without seeing its declaration, it's very hard to tell what's going on here - heck, it could even be a delegate (in which case the name isn't too bad after all).

The point is, if it's just data, then there's no reason to not pass it through the constructor. If it's work to do, then you've done the right thing: constructors shouldn't do work.

  Am I using too many Objects to handle these tasks, when a more procedural structure would do?

That's primarily opinion-based, but VB.NET, like C#, has an object-oriented paradigm; procedural "script-like" code would work, but if you want SOLID code, it just won't do.

Again, we have no idea what your objects are actually doing, so it's not possible to give you an answer here, but if your objects essentially are made of one or two one-liner methods, OOP is probably overkill.

  Is the naming of the Baker and Transmitter confusing because they differ from the Objects name?

I'm confused here. Baker is an object name:

Dim Baker As New PAN.Maker

And Transmitter is a type name:

Dim PANSender As New PAN.Transmitter(GetFTPDetails)

PAN looks like it's a class name, and Transmitter would be a nested type - I don't like this. If PAN is a namespace, it's a bad name and Transmitter shouldn't be fully-qualified like this.

Dim PAN As PANv1 = Baker.GetPAN()

The local variable PAN confuses things even further - is it the same PAN as seen in PAN.Transmitter? That makes Transmitter a shared method, and that's starting to smell.


In my Hangman game, one of my functions is correct but messy I got a ton of helpful tips last time I posted some code, so I thought I would come back to the well.

My function is deciding whether or not the secretWord has been guessed in my hangman game.  I am trying to accomplish this by taking a list, lettersGuessed, and comparing it to the char in each index of the string secretWord. My code works, but I feel as though it is not optimal nor formatted well.

def isWordGuessed(secretWord, lettersGuessed):
    '''
    secretWord: string, the word the user is guessing
    lettersGuessed: list, what letters have been guessed so far
    returns: boolean, True if all the letters of secretWord are in lettersGuessed;
      False otherwise
    '''
    chars = len(secretWord)
    place = 0
    while place < chars:
        if secretWord[0] in lettersGuessed:
            place += 1
            return isWordGuessed(secretWord[1:], lettersGuessed)
        else:
            return False
    return True

I tried to use a for loop (for i in secretWord:) originally, but I could not get my code to return both True and False.  It would only do one or the other, which is how I ended up with the while loop. It seems that while loops are discouraged/looked at as not very useful.  Is this correct?

Also, I am wondering if the recursive call is a good way of accomplishing the task.

Because we always return from the inside of the while, we'll never perform the loop more than once. Thus, your while actually acts like an if. Thus the value of place is not really used and you can get rid of it : if place < chars becomes if 0 < chars which is if chars which is if len(secretWord) which is really if secretWord because of the way Python considers the empty string as a false value.
Thus, you code could be :

def isWordGuessed(secretWord, lettersGuessed):
    if secretWord:
        if secretWord[0] in lettersGuessed:
            return isWordGuessed(secretWord[1:], lettersGuessed)
        else:
            return False
    return True

Which can be re-written as :

def isWordGuessed(secretWord, lettersGuessed):
    if secretWord:
        return secretWord[0] in lettersGuessed and isWordGuessed(secretWord[1:], lettersGuessed)
    return True

which is then nothing but :

def isWordGuessed(secretWord, lettersGuessed):
    return not secretWord or secretWord[0] in lettersGuessed and isWordGuessed(secretWord[1:], lettersGuessed)

Now, if I was to write the same function, because it is so easy to iterate on strings in Python, I'd probably avoid the recursion and so something like :

def isWordGuessed(secretWord, lettersGuessed):
    for letter in secretWord:
        if letter not in lettersGuessed:
            return False
    return True

Then, there might be a way to make this a one-liner but I find this to be pretty easy to understand and pretty efficient.