Never been to DZone Snippets before?

Snippets is a public source code repository. Easily build up your personal collection of code snippets, categorize them with tags / keywords, and share them with the world

« Newer Snippets
Older Snippets »
Showing 1-6 of 6 total  RSS 

Sort file by length of lines

Sort file by length of lines

cat $@ | awk '{ print length, $0 }' | sort -n | awk '{$1=""; print $0}'

cmp-length - compare the length of two series and return -1, 0, or 1, to support stable sorts by length

    ; Support func for stable sort comparator
    cmp-length: func [a [series!] b [series!] /local len-a len-b] [
        len-a: length? a
        len-b: length? b
        case [
            len-a < len-b [-1]
            len-a > len-b [1]
            len-a = len-b [0]
        ]
    ]

one-item? - simple wrapper to aid readability

one-item?: func [series] [1 = length? series]

Adjust Page Number when Page Length Changes

In a web page you are display a certain page worth of results. If you allow the user to change the page length, you'll need to find the new page number that contains the first item on the current page. This is VB.Net code.

intOldPageLength = GetPageLengthValue()

' if there is a new page length value
If intOldPageLength <> m_intPageLength Then

   ' adjust the page number to compensate
   intItemCount = ((m_intPage - 1) * intOldPageLength) + 1
   m_intPage = intItemCount \ m_intPageLength
   If intItemCount - (m_intPage * m_intPageLength) > 0 Then
      m_intPage = m_intPage + 1
   End If

End If


The index of the first item on the current page happens to be a count of the items up to that point. So assume we are paging that amount and use the last page as our new current page. We apply standard paging logic of items integer divided by page length to determine the number of pages and add one if there is a remainder.

There are a couple formulas at work here worth noting.
Index of Last Item on page:
page number * page length
Index of First Item on page:
(index of last item of previous page) + 1
((page number - 1) * page length) + 1
Page Count based on page length:
number of items [integer divide] page length
if there is a remainder to the above then add 1

Lenght between a dot and a line //JavaScript Function


Given a dot and a line, it returns the distance between them, the last parameter tells if the line should be considered infinite or if the function should respect its limits.

[UPDATED CODE AND HELP CAN BE FOUND HERE]


//+ Jonas Raoni Soares Silva
//@ http://jsfromhell.com/math/dot-line-length [v1.0]

dotLineLength = function( x, y, x0, y0, x1, y1, o ){
	function lineLength( x, y, x0, y0 ){
		return Math.sqrt( ( x -= x0 ) * x + ( y -= y0 ) * y );
	}
	if( o && !( o = function( x, y, x0, y0, x1, y1 ){
		if( !( x1 - x0 ) ) return { x: x0, y: y };
		else if( !( y1 - y0 ) ) return { x: x, y: y0 };
		var left, tg = -1 / ( ( y1 - y0 ) / ( x1 - x0 ) );
		return { x: left = ( x1 * ( x * tg - y + y0 ) + x0 * ( x * - tg + y - y1 ) ) / ( tg * ( x1 - x0 ) + y0 - y1 ), y: tg * left - tg * x + y };
	}( x, y, x0, y0, x1, y1 ), o.x >= Math.min( x0, x1 ) && o.x <= Math.max( x0, x1 ) && o.y >= Math.min( y0, y1 ) && o.y <= Math.max( y0, y1 ) ) ){
		var l1 = lineLength( x, y, x0, y0 ), l2 = lineLength( x, y, x1, y1 );
		return l1 > l2 ? l2 : l1;
	}
	else {
		var a = y0 - y1, b = x1 - x0, c = x0 * y1 - y0 * x1;
		return Math.abs( a * x + b * y + c ) / Math.sqrt( a * a + b * b );
	}
};

Line Length //JavaScript Function


Returns the length of a line.

[UPDATED CODE AND HELP CAN BE FOUND HERE]



//+ Jonas Raoni Soares Silva
//@ http://jsfromhell.com/math/line-length [v1.0]

lineLength = function( x, y, x0, y0 ){
	return Math.sqrt( ( x -= x0 ) * x + ( y -= y0 ) * y );
};
« Newer Snippets
Older Snippets »
Showing 1-6 of 6 total  RSS